aboutsummaryrefslogtreecommitdiff
path: root/file_test.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2026-01-08 04:29:18 +0700
committerShulhan <ms@kilabit.info>2026-01-08 04:29:18 +0700
commit2f6ebacaea7851be5c17a970514769dd3e9735e9 (patch)
treed34bcd794ae3946d1a5446e7554ec3cfeaa7c40b /file_test.go
parent161049a3996c574d521d6d3df55998028eb111c0 (diff)
downloadspdxconv-2f6ebacaea7851be5c17a970514769dd3e9735e9.tar.xz
all: implement conversion for SPDX-License-Identifier
If the file contains "SPDX-License-Identifier", it will not modify it. The program will move the identifier to the top of file after shebang. If the spdxconv.cfg contains match-license, and the pattern match with one of the line in the file, it will use the license_identifier instead of default one and insert it at the top, after shebang. If the files does not contains the identifier, it will insert new one based on default value in spdxconv.cfg file.
Diffstat (limited to 'file_test.go')
-rw-r--r--file_test.go107
1 files changed, 107 insertions, 0 deletions
diff --git a/file_test.go b/file_test.go
new file mode 100644
index 0000000..31e2a3d
--- /dev/null
+++ b/file_test.go
@@ -0,0 +1,107 @@
+// SPDX-License-Identifier: GPL-3.0-only
+// SPDX-FileCopyrightText: 2025 M. Shulhan <ms@kilabit.info>
+
+package spdxconv
+
+import (
+ "os"
+ "testing"
+
+ "git.sr.ht/~shulhan/pakakeh.go/lib/test"
+)
+
+func TestFile_all(t *testing.T) {
+ // Populate the test files from `testdata/file_test.txt`.
+
+ var testData *test.Data
+ var err error
+ testData, err = test.LoadData(`testdata/file_test.txt`)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var tempDir = `testdata/file/`
+ testData.ExtractInput(tempDir)
+ t.Chdir(tempDir)
+
+ // Inititalize the SPDXConv instance.
+
+ conv, err := New(`.`)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ for input, _ := range testData.Input {
+ if input == `spdxconv.cfg` {
+ continue
+ }
+
+ f, err := newFile(input, conv.cfg.MaxLineMatch)
+ if err != nil {
+ t.Fatal(err)
+ }
+ f.apply(conv)
+ err = f.write()
+ if err != nil {
+ t.Fatal(err)
+ }
+ got, err := os.ReadFile(input)
+ if err != nil {
+ t.Fatal(err)
+ }
+ test.Assert(t, input+`: after`,
+ string(testData.Output[input]), string(got))
+ }
+}
+
+func TestFile_detectComment(t *testing.T) {
+ type testCase struct {
+ topLines [][]byte
+ expFile file
+ }
+ listCase := []testCase{{
+ topLines: [][]byte{
+ []byte(`#!/bin/sh`),
+ },
+ expFile: file{
+ commentPrefix: `# `,
+ hasSheBang: true,
+ },
+ }, {
+ topLines: [][]byte{
+ []byte(`# comment`),
+ },
+ expFile: file{
+ commentPrefix: `# `,
+ },
+ }, {
+ topLines: [][]byte{
+ []byte(`// comment`),
+ },
+ expFile: file{
+ commentPrefix: `// `,
+ },
+ }, {
+ topLines: [][]byte{
+ []byte(`/*`),
+ },
+ expFile: file{
+ commentPrefix: `// `,
+ },
+ }, {
+ topLines: [][]byte{
+ []byte(`<!--`),
+ },
+ expFile: file{
+ commentPrefix: `<!-- `,
+ commentSuffix: ` -->`,
+ },
+ }}
+ for _, tc := range listCase {
+ f := file{
+ topLines: tc.topLines,
+ }
+ f.detectComment()
+ f.topLines = nil
+ test.Assert(t, string(tc.topLines[0]), tc.expFile, f)
+ }
+}