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