xref: /aosp_15_r20/build/make/tools/compliance/cmd/checkmetadata/checkmetadata_test.go (revision 9e94795a3d4ef5c1d47486f9a02bb378756cea8a)
1// Copyright 2022 Google LLC
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 main
16
17import (
18	"bytes"
19	"fmt"
20	"os"
21	"strings"
22	"testing"
23
24	"android/soong/tools/compliance"
25)
26
27func TestMain(m *testing.M) {
28	// Change into the parent directory before running the tests
29	// so they can find the testdata directory.
30	if err := os.Chdir(".."); err != nil {
31		fmt.Printf("failed to change to testdata directory: %s\n", err)
32		os.Exit(1)
33	}
34	os.Exit(m.Run())
35}
36
37func Test(t *testing.T) {
38	tests := []struct {
39		name           string
40		projects       []string
41		expectedStdout string
42	}{
43		{
44			name:           "1p",
45			projects:       []string{"firstparty"},
46			expectedStdout: "PASS -- parsed 1 project metadata files for 1 projects",
47		},
48		{
49			name:           "notice",
50			projects:       []string{"notice"},
51			expectedStdout: "PASS -- parsed 1 project metadata files for 1 projects",
52		},
53		{
54			name:           "1p+notice",
55			projects:       []string{"firstparty", "notice"},
56			expectedStdout: "PASS -- parsed 2 project metadata files for 2 projects",
57		},
58		{
59			name:           "reciprocal",
60			projects:       []string{"reciprocal"},
61			expectedStdout: "PASS -- parsed 1 project metadata files for 1 projects",
62		},
63		{
64			name:           "1p+notice+reciprocal",
65			projects:       []string{"firstparty", "notice", "reciprocal"},
66			expectedStdout: "PASS -- parsed 3 project metadata files for 3 projects",
67		},
68		{
69			name:           "restricted",
70			projects:       []string{"restricted"},
71			expectedStdout: "PASS -- parsed 1 project metadata files for 1 projects",
72		},
73		{
74			name:           "1p+notice+reciprocal+restricted",
75			projects:       []string{
76				"firstparty",
77				"notice",
78				"reciprocal",
79				"restricted",
80			},
81			expectedStdout: "PASS -- parsed 4 project metadata files for 4 projects",
82		},
83		{
84			name:           "proprietary",
85			projects:       []string{"proprietary"},
86			expectedStdout: "PASS -- parsed 1 project metadata files for 1 projects",
87		},
88		{
89			name:           "1p+notice+reciprocal+restricted+proprietary",
90			projects:       []string{
91				"firstparty",
92				"notice",
93				"reciprocal",
94				"restricted",
95				"proprietary",
96			},
97			expectedStdout: "PASS -- parsed 5 project metadata files for 5 projects",
98		},
99		{
100			name:           "missing1",
101			projects:       []string{"regressgpl1"},
102			expectedStdout: "PASS -- parsed 0 project metadata files for 1 projects",
103		},
104		{
105			name:           "1p+notice+reciprocal+restricted+proprietary+missing1",
106			projects:       []string{
107				"firstparty",
108				"notice",
109				"reciprocal",
110				"restricted",
111				"proprietary",
112				"regressgpl1",
113			},
114			expectedStdout: "PASS -- parsed 5 project metadata files for 6 projects",
115		},
116		{
117			name:           "missing2",
118			projects:       []string{"regressgpl2"},
119			expectedStdout: "PASS -- parsed 0 project metadata files for 1 projects",
120		},
121		{
122			name:           "1p+notice+reciprocal+restricted+proprietary+missing1+missing2",
123			projects:       []string{
124				"firstparty",
125				"notice",
126				"reciprocal",
127				"restricted",
128				"proprietary",
129				"regressgpl1",
130				"regressgpl2",
131			},
132			expectedStdout: "PASS -- parsed 5 project metadata files for 7 projects",
133		},
134		{
135			name:           "missing2+1p+notice+reciprocal+restricted+proprietary+missing1",
136			projects:       []string{
137				"regressgpl2",
138				"firstparty",
139				"notice",
140				"reciprocal",
141				"restricted",
142				"proprietary",
143				"regressgpl1",
144			},
145			expectedStdout: "PASS -- parsed 5 project metadata files for 7 projects",
146		},
147		{
148			name:           "missing2+1p+notice+missing1+reciprocal+restricted+proprietary",
149			projects:       []string{
150				"regressgpl2",
151				"firstparty",
152				"notice",
153				"regressgpl1",
154				"reciprocal",
155				"restricted",
156				"proprietary",
157			},
158			expectedStdout: "PASS -- parsed 5 project metadata files for 7 projects",
159		},
160	}
161	for _, tt := range tests {
162		t.Run(tt.name, func(t *testing.T) {
163			stdout := &bytes.Buffer{}
164			stderr := &bytes.Buffer{}
165
166			projects := make([]string, 0, len(tt.projects))
167			for _, project := range tt.projects {
168				projects = append(projects, "testdata/"+project)
169			}
170			err := checkProjectMetadata(stdout, stderr, compliance.GetFS(""), projects...)
171			if err != nil {
172				t.Fatalf("checkmetadata: error = %v, stderr = %v", err, stderr)
173				return
174			}
175			var actualStdout string
176			for _, s := range strings.Split(stdout.String(), "\n") {
177				ts := strings.TrimLeft(s, " \t")
178				if len(ts) < 1 {
179					continue
180				}
181				if len(actualStdout) > 0 {
182					t.Errorf("checkmetadata: unexpected multiple output lines %q, want %q", actualStdout+"\n"+ts, tt.expectedStdout)
183				}
184				actualStdout = ts
185			}
186			if actualStdout != tt.expectedStdout {
187				t.Errorf("checkmetadata: unexpected stdout %q, want %q", actualStdout, tt.expectedStdout)
188			}
189		})
190	}
191}
192