gen: prepare for move to Unicode 11 automatically update build tags option to skip tests Change-Id: Ic1e40c1a8308ed3641ba219a6e55b76dee98581f Reviewed-on: https://go-review.googlesource.com/c/text/+/154440 Run-TryBot: Marcel van Lohuizen <mpvl@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
diff --git a/gen.go b/gen.go index 224eef9..51e6603 100644 --- a/gen.go +++ b/gen.go
@@ -34,6 +34,7 @@ verbose = flag.Bool("v", false, "verbose output") force = flag.Bool("force", false, "ignore failing dependencies") doCore = flag.Bool("core", false, "force an update to core") + skipTest = flag.Bool("skiptest", false, "skip tests") excludeList = flag.String("exclude", "", "comma-separated list of packages to exclude") @@ -209,6 +210,10 @@ return } + if *skipTest { + return + } + vprintf("=== TEST %s\n", pkg) args[0] = "test" cmd = exec.Command(filepath.Join(runtime.GOROOT(), "bin", "go"), args...)
diff --git a/internal/gen/code.go b/internal/gen/code.go index 8622c7f..55dd1ed 100644 --- a/internal/gen/code.go +++ b/internal/gen/code.go
@@ -66,7 +66,9 @@ func (w *CodeWriter) WriteVersionedGoFile(filename, pkg string) { tags := buildTags() if tags != "" { - filename = insertVersion(filename, UnicodeVersion()) + pattern := fileToPattern(filename) + updateBuildTags(pattern) + filename = fmt.Sprintf(pattern, UnicodeVersion()) } f, err := os.Create(filename) if err != nil {
diff --git a/internal/gen/gen.go b/internal/gen/gen.go index 13257d6..41c3268 100644 --- a/internal/gen/gen.go +++ b/internal/gen/gen.go
@@ -31,6 +31,7 @@ "os" "path" "path/filepath" + "regexp" "strings" "sync" "unicode" @@ -83,25 +84,21 @@ } var tags = []struct{ version, buildTags string }{ - {"10.0.0", "go1.10"}, - {"", "!go1.10"}, + {"9.0.0", "!go1.10"}, + {"10.0.0", "go1.10, !go1.13"}, + {"11.0.0", "go1.13"}, } // buildTags reports the build tags used for the current Unicode version. func buildTags() string { v := UnicodeVersion() - for _, x := range tags { - // We should do a numeric comparison, but including the collate package - // would create an import cycle. We approximate it by assuming that - // longer version strings are later. - if len(x.version) <= len(v) { - return x.buildTags - } - if len(x.version) == len(v) && x.version <= v { - return x.buildTags + for _, e := range tags { + if e.version == v { + return e.buildTags } } - return tags[0].buildTags + log.Fatalf("Unknown build tags for Unicode version %q.", v) + return "" } // IsLocal reports whether data files are available locally. @@ -269,12 +266,29 @@ } } -func insertVersion(filename, version string) string { +func fileToPattern(filename string) string { suffix := ".go" if strings.HasSuffix(filename, "_test.go") { suffix = "_test.go" } - return fmt.Sprint(filename[:len(filename)-len(suffix)], version, suffix) + prefix := filename[:len(filename)-len(suffix)] + return fmt.Sprint(prefix, "%s", suffix) +} + +func updateBuildTags(pattern string) { + for _, t := range tags { + oldFile := fmt.Sprintf(pattern, t.version) + b, err := ioutil.ReadFile(oldFile) + if err != nil { + continue + } + build := fmt.Sprintf("// +build %s", t.buildTags) + b = regexp.MustCompile(`// \+build .*`).ReplaceAll(b, []byte(build)) + err = ioutil.WriteFile(oldFile, b, 0644) + if err != nil { + log.Fatal(err) + } + } } // WriteVersionedGoFile prepends a standard file comment, adds build tags to @@ -282,16 +296,16 @@ // the given bytes, applies gofmt, and writes them to a file with the given // name. It will call log.Fatal if there are any errors. func WriteVersionedGoFile(filename, pkg string, b []byte) { - tags := buildTags() - if tags != "" { - filename = insertVersion(filename, UnicodeVersion()) - } + pattern := fileToPattern(filename) + updateBuildTags(pattern) + filename = fmt.Sprintf(pattern, UnicodeVersion()) + w, err := os.Create(filename) if err != nil { log.Fatalf("Could not create file %s: %v", filename, err) } defer w.Close() - if _, err = WriteGo(w, pkg, tags, b); err != nil { + if _, err = WriteGo(w, pkg, buildTags(), b); err != nil { log.Fatalf("Error writing file %s: %v", filename, err) } }