xref: /aosp_15_r20/external/bazelbuild-rules_python/gazelle/manifest/manifest_test.go (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
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
15package manifest_test
16
17import (
18	"bytes"
19	"log"
20	"os"
21	"reflect"
22	"strings"
23	"testing"
24
25	"github.com/bazelbuild/rules_python/gazelle/manifest"
26)
27
28var modulesMapping = manifest.ModulesMapping{
29	"arrow": "arrow",
30}
31
32const pipDepsRepositoryName = "test_repository_name"
33
34func TestFile(t *testing.T) {
35	t.Run("EncodeWithIntegrity", func(t *testing.T) {
36		f := manifest.NewFile(&manifest.Manifest{
37			ModulesMapping:        modulesMapping,
38			PipDepsRepositoryName: pipDepsRepositoryName,
39		})
40		var b bytes.Buffer
41		manifestGeneratorHashFile := strings.NewReader("")
42		requirements, err := os.Open("testdata/requirements.txt")
43		if err != nil {
44			log.Println(err)
45			t.FailNow()
46		}
47		defer requirements.Close()
48		if err := f.EncodeWithIntegrity(&b, manifestGeneratorHashFile, requirements); err != nil {
49			log.Println(err)
50			t.FailNow()
51		}
52		expected, err := os.ReadFile("testdata/gazelle_python.yaml")
53		if err != nil {
54			log.Println(err)
55			t.FailNow()
56		}
57		if !bytes.Equal(expected, b.Bytes()) {
58			log.Printf("encoded manifest doesn't match expected output: %v\n", b.String())
59			t.FailNow()
60		}
61	})
62	t.Run("Decode", func(t *testing.T) {
63		f := manifest.NewFile(&manifest.Manifest{})
64		if err := f.Decode("testdata/gazelle_python.yaml"); err != nil {
65			log.Println(err)
66			t.FailNow()
67		}
68		if !reflect.DeepEqual(modulesMapping, f.Manifest.ModulesMapping) {
69			log.Println("decoded modules_mapping doesn't match expected value")
70			t.FailNow()
71		}
72		if f.Manifest.PipDepsRepositoryName != pipDepsRepositoryName {
73			log.Println("decoded pip repository name doesn't match expected value")
74			t.FailNow()
75		}
76	})
77	t.Run("VerifyIntegrity", func(t *testing.T) {
78		f := manifest.NewFile(&manifest.Manifest{})
79		if err := f.Decode("testdata/gazelle_python.yaml"); err != nil {
80			log.Println(err)
81			t.FailNow()
82		}
83		manifestGeneratorHashFile := strings.NewReader("")
84		requirements, err := os.Open("testdata/requirements.txt")
85		if err != nil {
86			log.Println(err)
87			t.FailNow()
88		}
89		defer requirements.Close()
90		valid, err := f.VerifyIntegrity(manifestGeneratorHashFile, requirements)
91		if err != nil {
92			log.Println(err)
93			t.FailNow()
94		}
95		if !valid {
96			log.Println("decoded manifest file is not valid")
97			t.FailNow()
98		}
99	})
100}
101