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