aboutsummaryrefslogtreecommitdiff
path: root/gazelle/manifest/test/test.go
blob: 8b580b14fc1dd6766759d60f54471f780d8502cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
test.go is a program that asserts the Gazelle YAML manifest is up-to-date in
regards to the requirements.txt.

It re-hashes the requirements.txt and compares it to the recorded one in the
existing generated Gazelle manifest.
*/
package main

import (
	"flag"
	"log"
	"os"
	"path/filepath"

	"github.com/bazelbuild/rules_python/gazelle/manifest"
)

func main() {
	var manifestGeneratorHashPath string
	var requirementsPath string
	var manifestPath string
	flag.StringVar(
		&manifestGeneratorHashPath,
		"manifest-generator-hash",
		"",
		"The file containing the hash for the source code of the manifest generator."+
			"This is important to force manifest updates when the generator logic changes.")
	flag.StringVar(
		&requirementsPath,
		"requirements",
		"",
		"The requirements.txt file.")
	flag.StringVar(
		&manifestPath,
		"manifest",
		"",
		"The manifest YAML file.")
	flag.Parse()

	if requirementsPath == "" {
		log.Fatalln("ERROR: --requirements must be set")
	}

	if manifestPath == "" {
		log.Fatalln("ERROR: --manifest must be set")
	}

	manifestFile := new(manifest.File)
	if err := manifestFile.Decode(manifestPath); err != nil {
		log.Fatalf("ERROR: %v\n", err)
	}

	if manifestFile.Integrity == "" {
		log.Fatalln("ERROR: failed to find the Gazelle manifest file integrity")
	}

	manifestGeneratorHash, err := os.Open(manifestGeneratorHashPath)
	if err != nil {
		log.Fatalf("ERROR: %v\n", err)
	}
	defer manifestGeneratorHash.Close()

	requirements, err := os.Open(requirementsPath)
	if err != nil {
		log.Fatalf("ERROR: %v\n", err)
	}
	defer requirements.Close()

	valid, err := manifestFile.VerifyIntegrity(manifestGeneratorHash, requirements)
	if err != nil {
		log.Fatalf("ERROR: %v\n", err)
	}
	if !valid {
		manifestRealpath, err := filepath.EvalSymlinks(manifestPath)
		if err != nil {
			log.Fatalf("ERROR: %v\n", err)
		}
		log.Fatalf(
			"ERROR: %q is out-of-date, follow the intructions on this file for updating.\n",
			manifestRealpath)
	}
}