grim/convey

Parents f86527406a8b
Children bd8085fe9a80
Move the normalize function for the environment package to it's own package and add some basic unit tests. Refs #121
--- a/environment/environment.go Tue Sep 12 16:15:35 2017 -0500
+++ b/environment/environment.go Tue Sep 12 20:31:38 2017 -0500
@@ -19,18 +19,9 @@
import (
"os"
- "regexp"
"time"
)
-var (
- re *regexp.Regexp
-)
-
-func init() {
- re = regexp.MustCompile("[^._A-Za-z0-9-]")
-}
-
func Initialize() []string {
oldHome := os.Getenv("HOME")
os.Setenv("HOME", "/tmp")
@@ -45,7 +36,3 @@
return env
}
-
-func normalize(in string) string {
- return re.ReplaceAllLiteralString(in, "_")
-}
--- a/environment/git.go Tue Sep 12 16:15:35 2017 -0500
+++ b/environment/git.go Tue Sep 12 20:31:38 2017 -0500
@@ -21,6 +21,8 @@
"os"
"os/exec"
"strings"
+
+ "bitbucket.org/rw_grim/convey/normalize"
)
func addGitEnv(envvar string, args ...string) string {
@@ -41,7 +43,7 @@
// add the branch and a normalized version
branch := addGitEnv("GIT_BRANCH", "rev-parse", "--abbrev-ref", "HEAD")
- os.Setenv("GIT_BRANCH_NORMALIZED", normalize(branch))
+ os.Setenv("GIT_BRANCH_NORMALIZED", normalize.Normalize(branch))
return []string{"GIT_COMMIT", "GIT_COMMIT_SHORT", "GIT_BRANCH", "GIT_BRANCH_NORMALIZED"}
}
--- a/environment/hg.go Tue Sep 12 16:15:35 2017 -0500
+++ b/environment/hg.go Tue Sep 12 20:31:38 2017 -0500
@@ -20,6 +20,8 @@
import (
"os"
"os/exec"
+
+ "bitbucket.org/rw_grim/convey/normalize"
)
func addHGEnv(envvar, template string) string {
@@ -41,7 +43,7 @@
// add the branch and a normalized version
branch := addHGEnv("HG_BRANCH", "{branch}")
- os.Setenv("HG_BRANCH_NORMALIZED", normalize(branch))
+ os.Setenv("HG_BRANCH_NORMALIZED", normalize.Normalize(branch))
return []string{"HG_COMMIT", "HG_COMMIT_SHORT", "HG_BRANCH", "HG_BRANCH_NORMALIZED"}
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/normalize/normalize.go Tue Sep 12 20:31:38 2017 -0500
@@ -0,0 +1,34 @@
+/*
+ * Convey
+ * Copyright 2016-2017 Gary Kramlich <grim@reaperworld.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package normalize
+
+import (
+ "regexp"
+)
+
+var (
+ re *regexp.Regexp
+)
+
+func init() {
+ re = regexp.MustCompile("[^._A-Za-z0-9-]")
+}
+
+func Normalize(in string) string {
+ return re.ReplaceAllLiteralString(in, "_")
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/normalize/normalize_test.go Tue Sep 12 20:31:38 2017 -0500
@@ -0,0 +1,72 @@
+/*
+ * Convey
+ * Copyright 2016-2017 Gary Kramlich <grim@reaperworld.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package normalize
+
+import (
+ "testing"
+
+ "github.com/aphistic/sweet"
+ junit "github.com/aphistic/sweet-junit"
+ . "github.com/onsi/gomega"
+)
+
+type normalizeSuite struct{}
+
+func TestMain(m *testing.M) {
+ RegisterFailHandler(sweet.GomegaFail)
+
+ sweet.Run(m, func(s *sweet.S) {
+ s.RegisterPlugin(junit.NewPlugin())
+
+ s.AddSuite(&normalizeSuite{})
+ })
+}
+
+func (s *normalizeSuite) simpleTest(tests map[string]string) {
+ for in, out := range tests {
+ Expect(Normalize(in)).To(Equal(out))
+ }
+}
+
+func (s *normalizeSuite) TestNormalized(t sweet.T) {
+ s.simpleTest(map[string]string{
+ "": "",
+ "_": "_",
+ "abc_123": "abc_123",
+ "ABC.123": "ABC.123",
+ })
+}
+
+func (s *normalizeSuite) TestSpaces(t sweet.T) {
+ s.simpleTest(map[string]string{
+ " ": "_",
+ "foo bar": "foo_bar",
+ " foo": "_foo",
+ "bar ": "bar_",
+ " foo bar ": "_foo_bar_",
+ })
+}
+
+func (s *normalizeSuite) TestSymbols(t sweet.T) {
+ s.simpleTest(map[string]string{
+ "@": "_",
+ "!": "_",
+ "()": "__",
+ "/\\": "__",
+ })
+}