• 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
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	"arrow.__init__":  "arrow",
31	"arrow.api":       "arrow",
32	"arrow.arrow":     "arrow",
33	"arrow.factory":   "arrow",
34	"arrow.formatter": "arrow",
35	"arrow.locales":   "arrow",
36	"arrow.parser":    "arrow",
37	"arrow.util":      "arrow",
38}
39
40const pipDepsRepositoryName = "test_repository_name"
41
42func TestFile(t *testing.T) {
43	t.Run("Encode", func(t *testing.T) {
44		f := manifest.NewFile(&manifest.Manifest{
45			ModulesMapping:        modulesMapping,
46			PipDepsRepositoryName: pipDepsRepositoryName,
47		})
48		var b bytes.Buffer
49		manifestGeneratorHashFile := strings.NewReader("")
50		requirements, err := os.Open("testdata/requirements.txt")
51		if err != nil {
52			log.Println(err)
53			t.FailNow()
54		}
55		defer requirements.Close()
56		if err := f.Encode(&b, manifestGeneratorHashFile, requirements); err != nil {
57			log.Println(err)
58			t.FailNow()
59		}
60		expected, err := os.ReadFile("testdata/gazelle_python.yaml")
61		if err != nil {
62			log.Println(err)
63			t.FailNow()
64		}
65		if !bytes.Equal(expected, b.Bytes()) {
66			log.Printf("encoded manifest doesn't match expected output: %v\n", b.String())
67			t.FailNow()
68		}
69	})
70	t.Run("Decode", func(t *testing.T) {
71		f := manifest.NewFile(&manifest.Manifest{})
72		if err := f.Decode("testdata/gazelle_python.yaml"); err != nil {
73			log.Println(err)
74			t.FailNow()
75		}
76		if !reflect.DeepEqual(modulesMapping, f.Manifest.ModulesMapping) {
77			log.Println("decoded modules_mapping doesn't match expected value")
78			t.FailNow()
79		}
80		if f.Manifest.PipDepsRepositoryName != pipDepsRepositoryName {
81			log.Println("decoded pip repository name doesn't match expected value")
82			t.FailNow()
83		}
84	})
85	t.Run("VerifyIntegrity", func(t *testing.T) {
86		f := manifest.NewFile(&manifest.Manifest{})
87		if err := f.Decode("testdata/gazelle_python.yaml"); err != nil {
88			log.Println(err)
89			t.FailNow()
90		}
91		manifestGeneratorHashFile := strings.NewReader("")
92		requirements, err := os.Open("testdata/requirements.txt")
93		if err != nil {
94			log.Println(err)
95			t.FailNow()
96		}
97		defer requirements.Close()
98		valid, err := f.VerifyIntegrity(manifestGeneratorHashFile, requirements)
99		if err != nil {
100			log.Println(err)
101			t.FailNow()
102		}
103		if !valid {
104			log.Println("decoded manifest file is not valid")
105			t.FailNow()
106		}
107	})
108}
109