|
| 1 | +package licenses |
| 2 | + |
| 3 | +import ( |
| 4 | + "path/filepath" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + "testing/fstest" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestContent_reportOnly(t *testing.T) { |
| 13 | + report := "dep1 (v1.0.0) - MIT - https://example.com\n" |
| 14 | + fsys := fstest.MapFS{ |
| 15 | + "third-party/PLACEHOLDER": &fstest.MapFile{Data: []byte("placeholder")}, |
| 16 | + } |
| 17 | + |
| 18 | + actualContent := content(report, fsys, "third-party") |
| 19 | + |
| 20 | + require.True(t, strings.HasPrefix(actualContent, report), "expected output to start with report") |
| 21 | + require.NotContains(t, actualContent, "PLACEHOLDER") |
| 22 | + require.NotContains(t, actualContent, "====") |
| 23 | +} |
| 24 | + |
| 25 | +func TestContent_singleModule(t *testing.T) { |
| 26 | + report := "example.com/mod (v1.0.0) - MIT - https://example.com\n" |
| 27 | + fsys := fstest.MapFS{ |
| 28 | + "third-party/example.com/mod/LICENSE": &fstest.MapFile{ |
| 29 | + Data: []byte("MIT License\n\nCopyright (c) 2024"), |
| 30 | + }, |
| 31 | + } |
| 32 | + |
| 33 | + actualContent := content(report, fsys, "third-party") |
| 34 | + |
| 35 | + require.Contains(t, actualContent, filepath.FromSlash("example.com/mod")) |
| 36 | + require.Contains(t, actualContent, "MIT License") |
| 37 | +} |
| 38 | + |
| 39 | +func TestContent_multipleModulesSortedAlphabetically(t *testing.T) { |
| 40 | + report := "header\n" |
| 41 | + fsys := fstest.MapFS{ |
| 42 | + "third-party/github.com/zzz/pkg/LICENSE": &fstest.MapFile{ |
| 43 | + Data: []byte("ZZZ License"), |
| 44 | + }, |
| 45 | + "third-party/github.com/aaa/pkg/LICENSE": &fstest.MapFile{ |
| 46 | + Data: []byte("AAA License"), |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + actualContent := content(report, fsys, "third-party") |
| 51 | + |
| 52 | + aIdx := strings.Index(actualContent, filepath.FromSlash("github.com/aaa/pkg")) |
| 53 | + zIdx := strings.Index(actualContent, filepath.FromSlash("github.com/zzz/pkg")) |
| 54 | + require.NotEqual(t, -1, aIdx, "expected aaa module in output") |
| 55 | + require.NotEqual(t, -1, zIdx, "expected zzz module in output") |
| 56 | + require.Less(t, aIdx, zIdx, "expected modules to be sorted alphabetically") |
| 57 | +} |
| 58 | + |
| 59 | +func TestContent_licenseAndNoticeFiles(t *testing.T) { |
| 60 | + report := "header\n" |
| 61 | + fsys := fstest.MapFS{ |
| 62 | + "third-party/example.com/mod/LICENSE": &fstest.MapFile{ |
| 63 | + Data: []byte("Apache License 2.0"), |
| 64 | + }, |
| 65 | + "third-party/example.com/mod/NOTICE": &fstest.MapFile{ |
| 66 | + Data: []byte("Copyright 2024 Example Corp"), |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + actualContent := content(report, fsys, "third-party") |
| 71 | + |
| 72 | + require.Contains(t, actualContent, "Apache License 2.0") |
| 73 | + require.Contains(t, actualContent, "Copyright 2024 Example Corp") |
| 74 | +} |
| 75 | + |
| 76 | +func TestContent_emptyThirdPartyDir(t *testing.T) { |
| 77 | + report := "header\n" |
| 78 | + fsys := fstest.MapFS{ |
| 79 | + "third-party/empty": &fstest.MapFile{Data: []byte("")}, |
| 80 | + } |
| 81 | + |
| 82 | + actualContent := content(report, fsys, "third-party") |
| 83 | + |
| 84 | + require.True(t, strings.HasPrefix(actualContent, "header\n"), "expected output to start with report header") |
| 85 | +} |
0 commit comments