|
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | 15 | /* |
16 | | -test.go is a program that asserts the Gazelle YAML manifest is up-to-date in |
| 16 | +test.go is a unit test that asserts the Gazelle YAML manifest is up-to-date in |
17 | 17 | regards to the requirements.txt. |
18 | 18 |
|
19 | 19 | It re-hashes the requirements.txt and compares it to the recorded one in the |
20 | 20 | existing generated Gazelle manifest. |
21 | 21 | */ |
22 | | -package main |
| 22 | +package test |
23 | 23 |
|
24 | 24 | import ( |
25 | | - "flag" |
26 | | - "log" |
27 | 25 | "os" |
28 | 26 | "path/filepath" |
| 27 | + "testing" |
29 | 28 |
|
30 | 29 | "github.com/bazelbuild/rules_python/gazelle/manifest" |
31 | 30 | ) |
32 | 31 |
|
33 | | -func main() { |
34 | | - var manifestGeneratorHashPath string |
35 | | - var requirementsPath string |
36 | | - var manifestPath string |
37 | | - flag.StringVar( |
38 | | - &manifestGeneratorHashPath, |
39 | | - "manifest-generator-hash", |
40 | | - "", |
41 | | - "The file containing the hash for the source code of the manifest generator."+ |
42 | | - "This is important to force manifest updates when the generator logic changes.") |
43 | | - flag.StringVar( |
44 | | - &requirementsPath, |
45 | | - "requirements", |
46 | | - "", |
47 | | - "The requirements.txt file.") |
48 | | - flag.StringVar( |
49 | | - &manifestPath, |
50 | | - "manifest", |
51 | | - "", |
52 | | - "The manifest YAML file.") |
53 | | - flag.Parse() |
54 | | - |
| 32 | +func TestGazelleManifestIsUpdated(t *testing.T) { |
| 33 | + requirementsPath := os.Getenv("_TEST_REQUIREMENTS") |
55 | 34 | if requirementsPath == "" { |
56 | | - log.Fatalln("ERROR: --requirements must be set") |
| 35 | + t.Fatalf("_TEST_REQUIREMENTS must be set") |
57 | 36 | } |
58 | 37 |
|
| 38 | + manifestPath := os.Getenv("_TEST_MANIFEST") |
59 | 39 | if manifestPath == "" { |
60 | | - log.Fatalln("ERROR: --manifest must be set") |
| 40 | + t.Fatalf("_TEST_MANIFEST must be set") |
61 | 41 | } |
62 | 42 |
|
63 | 43 | manifestFile := new(manifest.File) |
64 | 44 | if err := manifestFile.Decode(manifestPath); err != nil { |
65 | | - log.Fatalf("ERROR: %v\n", err) |
| 45 | + t.Fatalf("decoding manifest file: %v", err) |
66 | 46 | } |
67 | 47 |
|
68 | 48 | if manifestFile.Integrity == "" { |
69 | | - log.Fatalln("ERROR: failed to find the Gazelle manifest file integrity") |
| 49 | + t.Fatal("failed to find the Gazelle manifest file integrity") |
70 | 50 | } |
71 | 51 |
|
| 52 | + manifestGeneratorHashPath := os.Getenv("_TEST_MANIFEST_GENERATOR_HASH") |
72 | 53 | manifestGeneratorHash, err := os.Open(manifestGeneratorHashPath) |
73 | 54 | if err != nil { |
74 | | - log.Fatalf("ERROR: %v\n", err) |
| 55 | + t.Fatalf("opening %q: %v", manifestGeneratorHashPath, err) |
75 | 56 | } |
76 | 57 | defer manifestGeneratorHash.Close() |
77 | 58 |
|
78 | 59 | requirements, err := os.Open(requirementsPath) |
79 | 60 | if err != nil { |
80 | | - log.Fatalf("ERROR: %v\n", err) |
| 61 | + t.Fatalf("opening %q: %v", requirementsPath, err) |
81 | 62 | } |
82 | 63 | defer requirements.Close() |
83 | 64 |
|
84 | 65 | valid, err := manifestFile.VerifyIntegrity(manifestGeneratorHash, requirements) |
85 | 66 | if err != nil { |
86 | | - log.Fatalf("ERROR: %v\n", err) |
| 67 | + t.Fatalf("verifying integrity: %v", err) |
87 | 68 | } |
88 | 69 | if !valid { |
89 | 70 | manifestRealpath, err := filepath.EvalSymlinks(manifestPath) |
90 | 71 | if err != nil { |
91 | | - log.Fatalf("ERROR: %v\n", err) |
| 72 | + t.Fatalf("evaluating symlink %q: %v", manifestPath, err) |
92 | 73 | } |
93 | | - log.Fatalf( |
94 | | - "ERROR: %q is out-of-date. Follow the update instructions in that file to resolve this.\n", |
| 74 | + t.Errorf( |
| 75 | + "%q is out-of-date. Follow the update instructions in that file to resolve this", |
95 | 76 | manifestRealpath) |
96 | 77 | } |
97 | 78 | } |
0 commit comments