package_index: allow chromeos project
Also fixed the deprecation warning for ioutil.
Bug: b/346352545
Change-Id: I57b98e7611c3e93d21df373438a570267e64140f
Reviewed-on: https://chromium-review.googlesource.com/c/infra/infra/+/5632226
Commit-Queue: Yiwei Zhang <yiwzhang@google.com>
Auto-Submit: Yiwei Zhang <yiwzhang@google.com>
Commit-Queue: Josip Sokcevic <sokcevic@chromium.org>
Reviewed-by: Josip Sokcevic <sokcevic@chromium.org>
Cr-Commit-Position: refs/heads/main@{#66245}
diff --git a/go/src/infra/cmd/package_index/main.go b/go/src/infra/cmd/package_index/main.go
index 13421e9..3daad02 100644
--- a/go/src/infra/cmd/package_index/main.go
+++ b/go/src/infra/cmd/package_index/main.go
@@ -9,9 +9,11 @@
"flag"
"os"
"path/filepath"
+ "strings"
"sync"
"time"
+ "go.chromium.org/luci/common/data/stringset"
"go.chromium.org/luci/common/logging"
"go.chromium.org/luci/common/logging/gologger"
@@ -37,14 +39,20 @@
outDirFlag = flag.String("out_dir", "src/out/Debug", "Output directory from which compilation is run.")
filepathsFlag = flag.Bool("keep_filepaths_files", false, "Keep the .filepaths files used for index pack generation.")
verboseFlag = flag.Bool("verbose", false, "Print the details of every file being written to the index pack.")
+
+ allowedProjects = stringset.NewFromSlice(
+ "chromium",
+ "chromiumos",
+ "chrome",
+ "chromeos")
)
// validateFlags checks that the required flags are present.
func validateFlags(ctx context.Context) {
flagErr := false
- if *projectFlag != "chromium" && *projectFlag != "chrome" && *projectFlag != "chromiumos" {
- logging.Errorf(ctx, "project not supported. 'chromium', 'chrome', and 'chromiumos' are the only supported projects.")
+ if !allowedProjects.Has(*projectFlag) {
+ logging.Errorf(ctx, "project not supported. [%s] are the only supported projects.", strings.Join(allowedProjects.ToSortedSlice(), ", "))
flagErr = true
}
diff --git a/go/src/infra/cmd/package_index/utils.go b/go/src/infra/cmd/package_index/utils.go
index 37aa101..3bcc2d9 100644
--- a/go/src/infra/cmd/package_index/utils.go
+++ b/go/src/infra/cmd/package_index/utils.go
@@ -1,9 +1,12 @@
+// Copyright 2020 The Chromium Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
package main
import (
"context"
"fmt"
- "io/ioutil"
"os"
"path"
"path/filepath"
@@ -158,7 +161,7 @@
return imports
}
- contents, err := ioutil.ReadFile(fpath)
+ contents, err := os.ReadFile(fpath)
if err != nil {
panic(fmt.Sprintf("Cannot read file %s: %v", fpath, err))
}
@@ -207,7 +210,7 @@
// * gen/${board}/src/out/${board}/
//
// For references to work correctly, set vname to point to files in the repo.
- if *projectFlag == "chromiumos" {
+ if isProjectCros(*projectFlag) {
chrootPathIndex := strings.Index(filepath, "cache/cros_chroot/")
if chrootPathIndex >= 0 {
// Strip everything up until and including "cache/cros_chroot/"
@@ -241,3 +244,8 @@
}
return false
}
+
+// isProjectCros checks if the project string represents chromeos.
+func isProjectCros(proj string) bool {
+ return proj == "chromiumos" || proj == "chromeos"
+}
diff --git a/go/src/infra/cmd/package_index/utils_test.go b/go/src/infra/cmd/package_index/utils_test.go
index 22d8b8f..5ab734c 100644
--- a/go/src/infra/cmd/package_index/utils_test.go
+++ b/go/src/infra/cmd/package_index/utils_test.go
@@ -1,8 +1,12 @@
+// Copyright 2020 The Chromium Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
package main
import (
"context"
- "io/ioutil"
+ "fmt"
"os"
"path/filepath"
"regexp"
@@ -19,38 +23,26 @@
t.Parallel()
Convey("Remove filepaths files", t, func() {
ctx := context.Background()
- tmpdir, err := ioutil.TempDir("", "")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(tmpdir)
+ tmpdir := t.TempDir()
Convey("Remove filepaths with files", func() {
// File setup
f, err := os.Create(filepath.Join(tmpdir, "foo.filepaths"))
- if err != nil {
- t.Fatal(err)
- }
+ So(err, ShouldBeNil)
f.Close()
fpath, err := filepath.Abs(f.Name())
if err != nil {
t.Fatal(err)
}
- nested, err := ioutil.TempDir(tmpdir, "")
- if err != nil {
- t.Fatal(err)
- }
+ nested, err := os.MkdirTemp(tmpdir, "")
+ So(err, ShouldBeNil)
b, err := os.Create(filepath.Join(nested, "bar.filepaths"))
- if err != nil {
- t.Fatal(err)
- }
+ So(err, ShouldBeNil)
b.Close()
bpath, err := filepath.Abs(b.Name())
- if err != nil {
- t.Fatal(err)
- }
+ So(err, ShouldBeNil)
removeFilepathsFiles(ctx, tmpdir)
@@ -180,25 +172,16 @@
re := regexp.MustCompile(`(?m)^\s*import\s*(?:weak|public)?\s*"([^"]*)\s*";`)
// File setup
- tmpdir, err := ioutil.TempDir("", "")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(tmpdir)
-
+ tmpdir := t.TempDir()
importPaths := []string{tmpdir}
f, err := os.Create(tmpdir + "/test.proto")
- if err != nil {
- t.Fatal(err)
- }
+ So(err, ShouldBeNil)
defer f.Close()
f.WriteString("import \"foo.proto\";\nimport weak \"bar.proto\";\n")
fpath, err := filepath.Abs(f.Name())
- if err != nil {
- t.Fatal(err)
- }
+ So(err, ShouldBeNil)
Convey("File doesn't exist", func() {
p := "/path/to/no/file"
@@ -332,49 +315,51 @@
})
})
- Convey("With ChromiumOS", func() {
- *projectFlag = "chromiumos"
+ for _, proj := range []string{"chromiumos", "chromeos"} {
+ *projectFlag = proj
+ Convey(fmt.Sprintf("With %s", proj), func() {
+ Convey("Filepath has chroot prefix", func() {
+ p := "../../../../cache/cros_chroot/chroot/build/amd64-generic/rest/of/path"
+ setVnameForFile(&vnameProto, p, defaultCorpus)
- Convey("Filepath has chroot prefix", func() {
- p := "../../../../cache/cros_chroot/chroot/build/amd64-generic/rest/of/path"
- setVnameForFile(&vnameProto, p, defaultCorpus)
+ Convey("Should prefix vnameProto with path to gen files", func() {
+ So(vnameProto.Path, ShouldEqual, "gen/amd64-generic/chroot/build/amd64-generic/rest/of/path")
+ })
+ })
- Convey("Should prefix vnameProto with path to gen files", func() {
- So(vnameProto.Path, ShouldEqual, "gen/amd64-generic/chroot/build/amd64-generic/rest/of/path")
+ Convey("Filepath has out dir prefix", func() {
+ p := "src/out/amd64-generic/rest/of/path"
+ setVnameForFile(&vnameProto, p, defaultCorpus)
+
+ Convey("Should prefix vnameProto with path to gen files", func() {
+ So(vnameProto.Path, ShouldEqual, "gen/amd64-generic/src/out/amd64-generic/rest/of/path")
+ })
+ })
+
+ Convey("Filepath has no special corpus with src prefix", func() {
+ p := "src/build/rest/of/path"
+ setVnameForFile(&vnameProto, p, defaultCorpus)
+
+ Convey("Should not modify path", func() {
+ So(vnameProto.Path, ShouldEqual, p)
+ So(vnameProto.Root, ShouldEqual, vnameProtoRoot)
+ So(vnameProto.Corpus, ShouldEqual, defaultCorpus)
+ })
+ })
+
+ Convey("Filepath has no special corpus without src prefix", func() {
+ p := "foo/build/rest/of/path"
+ setVnameForFile(&vnameProto, p, defaultCorpus)
+
+ Convey("Should not modify path", func() {
+ So(vnameProto.Path, ShouldEqual, p)
+ So(vnameProto.Root, ShouldEqual, vnameProtoRoot)
+ So(vnameProto.Corpus, ShouldEqual, defaultCorpus)
+ })
})
})
- Convey("Filepath has out dir prefix", func() {
- p := "src/out/amd64-generic/rest/of/path"
- setVnameForFile(&vnameProto, p, defaultCorpus)
-
- Convey("Should prefix vnameProto with path to gen files", func() {
- So(vnameProto.Path, ShouldEqual, "gen/amd64-generic/src/out/amd64-generic/rest/of/path")
- })
- })
-
- Convey("Filepath has no special corpus with src prefix", func() {
- p := "src/build/rest/of/path"
- setVnameForFile(&vnameProto, p, defaultCorpus)
-
- Convey("Should not modify path", func() {
- So(vnameProto.Path, ShouldEqual, p)
- So(vnameProto.Root, ShouldEqual, vnameProtoRoot)
- So(vnameProto.Corpus, ShouldEqual, defaultCorpus)
- })
- })
-
- Convey("Filepath has no special corpus without src prefix", func() {
- p := "foo/build/rest/of/path"
- setVnameForFile(&vnameProto, p, defaultCorpus)
-
- Convey("Should not modify path", func() {
- So(vnameProto.Path, ShouldEqual, p)
- So(vnameProto.Root, ShouldEqual, vnameProtoRoot)
- So(vnameProto.Corpus, ShouldEqual, defaultCorpus)
- })
- })
- })
+ }
})
}
diff --git a/go/src/infra/cmd/package_index/utils_unix_test.go b/go/src/infra/cmd/package_index/utils_unix_test.go
index 3f6b81a..04f1288 100644
--- a/go/src/infra/cmd/package_index/utils_unix_test.go
+++ b/go/src/infra/cmd/package_index/utils_unix_test.go
@@ -1,3 +1,7 @@
+// Copyright 2020 The Chromium Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
//go:build !windows
// +build !windows