• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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	"testing"
20)
21
22func Test_doit(t *testing.T) {
23	input := `
24[
25  {
26    "rule": "@//build/soong/licenses:Android-Apache-2.0",
27    "license_kinds": [
28      {
29        "target": "@//build/soong/licenses:SPDX-license-identifier-Apache-2.0",
30        "name": "SPDX-license-identifier-Apache-2.0",
31        "conditions": ["notice"]
32      }
33    ],
34    "copyright_notice": "Copyright (C) The Android Open Source Project",
35    "package_name": "Discombobulator",
36    "package_url": null,
37    "package_version": null,
38    "license_text": "../../testdata/NOTICE_LICENSE",
39	"licensees": [
40        "@//bionic/libc:libc_bionic_ndk",
41		"@//system/logging/liblog:liblog"
42    ]
43 },
44 {
45    "rule": "@//external/scudo:external_scudo_license",
46    "license_kinds": [
47      {
48        "target": "@//build/soong/licenses:SPDX-license-identifier-Apache-2.0",
49        "name": "SPDX-license-identifier-Apache-2.0",
50        "conditions": ["notice"]
51      }
52    ],
53    "copyright_notice": "",
54    "package_name": "Scudo Standalone",
55    "package_url": null,
56    "package_version": null,
57	"licensees": [
58        "@//external/scudo:foo"
59    ]
60  }
61]
62`
63	tests := []struct {
64		name        string
65		in          string
66		listTargets bool
67		want        string
68	}{
69		{
70			name:        "ListTargets",
71			in:          input,
72			listTargets: true,
73			want: `<!DOCTYPE html>
74<html>
75  <head>
76    <style type="text/css">
77      body { padding: 2px; margin: 0; }
78      .license { background-color: seashell; margin: 1em;}
79      pre { padding: 1em; }</style></head>
80  <body>
81    The following software has been included in this product and contains the license and notice as shown below.<p>
82    <strong>Discombobulator</strong><br>Copyright Notice: Copyright (C) The Android Open Source Project<br><a href=#b9835e4a000fb18a4c8970690daa3b95>License</a><br>
83    Used by: @//bionic/libc:libc_bionic_ndk @//system/logging/liblog:liblog<hr>
84    <strong>Scudo Standalone</strong><br>
85    Used by: @//external/scudo:foo<hr>
86    <div id="b9835e4a000fb18a4c8970690daa3b95" class="license"><pre>neque porro quisquam est qui do-
87lorem ipsum
88
89    </pre></div>
90  </body>
91</html>
92`,
93		},
94		{
95			name:        "NoTargets",
96			in:          input,
97			listTargets: false,
98			want: `<!DOCTYPE html>
99<html>
100  <head>
101    <style type="text/css">
102      body { padding: 2px; margin: 0; }
103      .license { background-color: seashell; margin: 1em;}
104      pre { padding: 1em; }</style></head>
105  <body>
106    The following software has been included in this product and contains the license and notice as shown below.<p>
107    <strong>Discombobulator</strong><br>Copyright Notice: Copyright (C) The Android Open Source Project<br><a href=#b9835e4a000fb18a4c8970690daa3b95>License</a><br>
108    <strong>Scudo Standalone</strong><br>
109    <div id="b9835e4a000fb18a4c8970690daa3b95" class="license"><pre>neque porro quisquam est qui do-
110lorem ipsum
111
112    </pre></div>
113  </body>
114</html>
115`,
116		},
117	}
118	for _, tt := range tests {
119		t.Run(tt.name, func(t *testing.T) {
120			buf := bytes.Buffer{}
121			newGenerator(tt.in).generate(&buf, tt.listTargets)
122			got := buf.String()
123			if got != tt.want {
124				t.Errorf("doit() = %v, want %v", got, tt.want)
125			}
126		})
127	}
128}
129