1*60517a1eSAndroid Build Coastguard Worker// Copyright 2023 The Bazel Authors. All rights reserved. 2*60517a1eSAndroid Build Coastguard Worker// 3*60517a1eSAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License"); 4*60517a1eSAndroid Build Coastguard Worker// you may not use this file except in compliance with the License. 5*60517a1eSAndroid Build Coastguard Worker// You may obtain a copy of the License at 6*60517a1eSAndroid Build Coastguard Worker// 7*60517a1eSAndroid Build Coastguard Worker// http://www.apache.org/licenses/LICENSE-2.0 8*60517a1eSAndroid Build Coastguard Worker// 9*60517a1eSAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software 10*60517a1eSAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS, 11*60517a1eSAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*60517a1eSAndroid Build Coastguard Worker// See the License for the specific language governing permissions and 13*60517a1eSAndroid Build Coastguard Worker// limitations under the License. 14*60517a1eSAndroid Build Coastguard Worker 15*60517a1eSAndroid Build Coastguard Worker/* 16*60517a1eSAndroid Build Coastguard Workertest.go is a unit test that asserts the Gazelle YAML manifest is up-to-date in 17*60517a1eSAndroid Build Coastguard Workerregards to the requirements.txt. 18*60517a1eSAndroid Build Coastguard Worker 19*60517a1eSAndroid Build Coastguard WorkerIt re-hashes the requirements.txt and compares it to the recorded one in the 20*60517a1eSAndroid Build Coastguard Workerexisting generated Gazelle manifest. 21*60517a1eSAndroid Build Coastguard Worker*/ 22*60517a1eSAndroid Build Coastguard Workerpackage test 23*60517a1eSAndroid Build Coastguard Worker 24*60517a1eSAndroid Build Coastguard Workerimport ( 25*60517a1eSAndroid Build Coastguard Worker "os" 26*60517a1eSAndroid Build Coastguard Worker "path/filepath" 27*60517a1eSAndroid Build Coastguard Worker "testing" 28*60517a1eSAndroid Build Coastguard Worker 29*60517a1eSAndroid Build Coastguard Worker "github.com/bazelbuild/rules_go/go/runfiles" 30*60517a1eSAndroid Build Coastguard Worker "github.com/bazelbuild/rules_python/gazelle/manifest" 31*60517a1eSAndroid Build Coastguard Worker) 32*60517a1eSAndroid Build Coastguard Worker 33*60517a1eSAndroid Build Coastguard Workerfunc TestGazelleManifestIsUpdated(t *testing.T) { 34*60517a1eSAndroid Build Coastguard Worker requirementsPath := os.Getenv("_TEST_REQUIREMENTS") 35*60517a1eSAndroid Build Coastguard Worker if requirementsPath == "" { 36*60517a1eSAndroid Build Coastguard Worker t.Fatal("_TEST_REQUIREMENTS must be set") 37*60517a1eSAndroid Build Coastguard Worker } 38*60517a1eSAndroid Build Coastguard Worker 39*60517a1eSAndroid Build Coastguard Worker manifestPath := os.Getenv("_TEST_MANIFEST") 40*60517a1eSAndroid Build Coastguard Worker if manifestPath == "" { 41*60517a1eSAndroid Build Coastguard Worker t.Fatal("_TEST_MANIFEST must be set") 42*60517a1eSAndroid Build Coastguard Worker } 43*60517a1eSAndroid Build Coastguard Worker 44*60517a1eSAndroid Build Coastguard Worker manifestFile := new(manifest.File) 45*60517a1eSAndroid Build Coastguard Worker if err := manifestFile.Decode(manifestPath); err != nil { 46*60517a1eSAndroid Build Coastguard Worker t.Fatalf("decoding manifest file: %v", err) 47*60517a1eSAndroid Build Coastguard Worker } 48*60517a1eSAndroid Build Coastguard Worker 49*60517a1eSAndroid Build Coastguard Worker if manifestFile.Integrity == "" { 50*60517a1eSAndroid Build Coastguard Worker t.Fatal("failed to find the Gazelle manifest file integrity") 51*60517a1eSAndroid Build Coastguard Worker } 52*60517a1eSAndroid Build Coastguard Worker 53*60517a1eSAndroid Build Coastguard Worker manifestGeneratorHashPath, err := runfiles.Rlocation( 54*60517a1eSAndroid Build Coastguard Worker os.Getenv("_TEST_MANIFEST_GENERATOR_HASH")) 55*60517a1eSAndroid Build Coastguard Worker if err != nil { 56*60517a1eSAndroid Build Coastguard Worker t.Fatalf("failed to resolve runfiles path of manifest: %v", err) 57*60517a1eSAndroid Build Coastguard Worker } 58*60517a1eSAndroid Build Coastguard Worker 59*60517a1eSAndroid Build Coastguard Worker manifestGeneratorHash, err := os.Open(manifestGeneratorHashPath) 60*60517a1eSAndroid Build Coastguard Worker if err != nil { 61*60517a1eSAndroid Build Coastguard Worker t.Fatalf("opening %q: %v", manifestGeneratorHashPath, err) 62*60517a1eSAndroid Build Coastguard Worker } 63*60517a1eSAndroid Build Coastguard Worker defer manifestGeneratorHash.Close() 64*60517a1eSAndroid Build Coastguard Worker 65*60517a1eSAndroid Build Coastguard Worker requirements, err := os.Open(requirementsPath) 66*60517a1eSAndroid Build Coastguard Worker if err != nil { 67*60517a1eSAndroid Build Coastguard Worker t.Fatalf("opening %q: %v", requirementsPath, err) 68*60517a1eSAndroid Build Coastguard Worker } 69*60517a1eSAndroid Build Coastguard Worker defer requirements.Close() 70*60517a1eSAndroid Build Coastguard Worker 71*60517a1eSAndroid Build Coastguard Worker valid, err := manifestFile.VerifyIntegrity(manifestGeneratorHash, requirements) 72*60517a1eSAndroid Build Coastguard Worker if err != nil { 73*60517a1eSAndroid Build Coastguard Worker t.Fatalf("verifying integrity: %v", err) 74*60517a1eSAndroid Build Coastguard Worker } 75*60517a1eSAndroid Build Coastguard Worker if !valid { 76*60517a1eSAndroid Build Coastguard Worker manifestRealpath, err := filepath.EvalSymlinks(manifestPath) 77*60517a1eSAndroid Build Coastguard Worker if err != nil { 78*60517a1eSAndroid Build Coastguard Worker t.Fatalf("evaluating symlink %q: %v", manifestPath, err) 79*60517a1eSAndroid Build Coastguard Worker } 80*60517a1eSAndroid Build Coastguard Worker t.Errorf( 81*60517a1eSAndroid Build Coastguard Worker "%q is out-of-date. Follow the update instructions in that file to resolve this", 82*60517a1eSAndroid Build Coastguard Worker manifestRealpath) 83*60517a1eSAndroid Build Coastguard Worker } 84*60517a1eSAndroid Build Coastguard Worker} 85