aboutsummaryrefslogtreecommitdiff
path: root/file_test.go
diff options
context:
space:
mode:
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)
+ }
+}