xref: /aosp_15_r20/external/bazelbuild-rules_go/go/tools/bzltestutil/xml_test.go (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1// Copyright 2020 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 bzltestutil
16
17import (
18	"bytes"
19	"io/ioutil"
20	"os"
21	"path/filepath"
22	"strings"
23	"testing"
24)
25
26func TestJSON2XML(t *testing.T) {
27	files, err := filepath.Glob("testdata/*.json")
28	if err != nil {
29		t.Fatal(err)
30	}
31
32	for _, file := range files {
33		name := strings.TrimSuffix(filepath.Base(file), ".json")
34		t.Run(name, func(t *testing.T) {
35			orig, err := os.Open(file)
36			if err != nil {
37				t.Fatal(err)
38			}
39			got, err := json2xml(orig, "pkg/testing")
40			if err != nil {
41				t.Fatal(err)
42			}
43
44			target := strings.TrimSuffix(file, ".json") + ".xml"
45			want, err := ioutil.ReadFile(target)
46			if err != nil {
47				t.Fatal(err)
48			}
49
50			if !bytes.Equal(got, want) {
51				t.Errorf("json2xml for %s does not match, got:\n%s\nwant:\n%s\n", name, string(got), string(want))
52			}
53		})
54	}
55}
56