aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--scm.go7
-rw-r--r--spdxconv.go33
-rw-r--r--spdxconv_test.go55
-rw-r--r--testdata/scan_no_scm/node_modules/dummy1
-rw-r--r--testdata/scan_no_scm/test.go3
-rw-r--r--testdata/scan_no_scm/test.html5
-rw-r--r--testdata/scan_no_scm/test.sh3
-rw-r--r--testdata/scan_no_scm/vendor/dummy1
8 files changed, 78 insertions, 30 deletions
diff --git a/scm.go b/scm.go
index d94ee18..1353625 100644
--- a/scm.go
+++ b/scm.go
@@ -8,9 +8,10 @@ type sourceCodeManagement interface {
IsIgnored(path string) bool
}
-// dummySCM is a no scm. It always return false on IsIgnored.
-type dummySCM struct{}
+// noSCM is a type to indicate working directory without SCM.
+// It always return false on IsIgnored.
+type noSCM struct{}
-func (scm *dummySCM) IsIgnored(path string) bool {
+func (scm *noSCM) IsIgnored(path string) bool {
return false
}
diff --git a/spdxconv.go b/spdxconv.go
index 5f28e5a..7222eb4 100644
--- a/spdxconv.go
+++ b/spdxconv.go
@@ -7,6 +7,7 @@ import (
"fmt"
"os"
"path/filepath"
+ "strings"
"git.sr.ht/~shulhan/pakakeh.go/lib/git"
"git.sr.ht/~shulhan/pakakeh.go/lib/ini"
@@ -16,6 +17,9 @@ import (
type SPDXConv struct {
scm sourceCodeManagement
+ // curDir contains the current working directory.
+ curDir string
+
// dir define the directory that will processed.
// `name` is single file inside `dir` to be processed.
// If `name` is empty means process all files inside `dir`.
@@ -56,7 +60,7 @@ func Apply(path string) (err error) {
return nil
}
-// New initialize new instance of SPDXConv
+// New initialize new instance of SPDXConv.
func New(path string) (conv *SPDXConv, err error) {
var logp = `New`
@@ -66,6 +70,14 @@ func New(path string) (conv *SPDXConv, err error) {
}
conv = &SPDXConv{}
+ conv.curDir, err = os.Getwd()
+ if err != nil {
+ return nil, fmt.Errorf(`%s: %w`, logp, err)
+ }
+ if !strings.HasPrefix(path, conv.curDir) {
+ return nil, fmt.Errorf(`%s: %s must be under the current directory`, logp, path)
+ }
+
var fi os.FileInfo
fi, err = os.Stat(path)
if err != nil {
@@ -78,12 +90,12 @@ func New(path string) (conv *SPDXConv, err error) {
conv.dir, conv.name = filepath.Split(path)
}
- err = conv.loadConfig(conv.dir)
+ err = conv.loadConfig(conv.curDir)
if err != nil {
return nil, fmt.Errorf(`%s: %w`, logp, err)
}
- err = conv.scanForSCM(conv.dir)
+ err = conv.scanForSCM(conv.dir, conv.curDir)
if err != nil {
return nil, fmt.Errorf(`%s: %w`, logp, err)
}
@@ -115,32 +127,35 @@ func (conv *SPDXConv) loadConfig(dir string) (err error) {
return nil
}
-// scanForSCM scan for source-code management (SCM) from directory `dir` up
-// until the root.
+// scanForSCM scan for source-code management (SCM) from directory `dir` until
+// the current working directory.
// Currently, only support git.
-func (conv *SPDXConv) scanForSCM(dir string) (err error) {
+func (conv *SPDXConv) scanForSCM(dir, curDir string) (err error) {
var scmDir string
var fi os.FileInfo
- for dir != `/` {
+ for strings.HasPrefix(dir, curDir) {
scmDir = filepath.Join(dir, `.git`)
fi, err = os.Stat(scmDir)
if err != nil {
if os.IsNotExist(err) {
+ dir = filepath.Dir(dir)
continue
}
return err
}
if !fi.IsDir() {
+ dir = filepath.Dir(dir)
continue
}
- conv.scmDir = dir
conv.scm, err = git.New(dir)
if err != nil {
return err
}
+ conv.scmDir = dir
return nil
}
- conv.scm = &dummySCM{}
+
+ conv.scm = &noSCM{}
return nil
}
diff --git a/spdxconv_test.go b/spdxconv_test.go
index 9c66aea..40bcc16 100644
--- a/spdxconv_test.go
+++ b/spdxconv_test.go
@@ -36,15 +36,19 @@ func TestNew(t *testing.T) {
var listCase = []testCase{{
dir: `.`,
exp: &SPDXConv{
+ curDir: tempDir,
dir: tempDir,
scmDir: tempDir,
scm: gitRoot,
},
+ }, {
+ dir: `/tmp`,
+ expError: `New: /tmp must be under the current directory`,
}}
var tc testCase
var conv *SPDXConv
for _, tc = range listCase {
- conv, err = New(`.`)
+ conv, err = New(tc.dir)
if err != nil {
test.Assert(t, tc.dir+`: error`, tc.expError, err.Error())
continue
@@ -128,25 +132,40 @@ func TestSPDXConv_scanFile(t *testing.T) {
}
func TestSPDXConv_scanFiles(t *testing.T) {
+ type testCase struct {
+ dir string
+ exp []string
+ }
+ t.Chdir(`testdata/`)
+ var listCase = []testCase{{
+ dir: `scan/`,
+ exp: []string{
+ `scan/.gitignore`,
+ `scan/test.go`,
+ `scan/test.html`,
+ `scan/test.sh`,
+ },
+ }, {
+ dir: `scan_no_scm/`,
+ exp: []string{
+ `scan_no_scm/test.go`,
+ `scan_no_scm/test.html`,
+ `scan_no_scm/test.sh`,
+ },
+ }}
+ var tc testCase
var conv *SPDXConv
var err error
-
- conv, err = New(`testdata/scan/`)
- if err != nil {
- t.Fatal(err)
- }
-
var got []string
- got, err = conv.scanFiles([]string{`testdata/scan/`})
- if err != nil {
- t.Fatal(err)
- }
-
- var exp = []string{
- `testdata/scan/.gitignore`,
- `testdata/scan/test.go`,
- `testdata/scan/test.html`,
- `testdata/scan/test.sh`,
+ for _, tc = range listCase {
+ conv, err = New(tc.dir)
+ if err != nil {
+ t.Fatal(err)
+ }
+ got, err = conv.scanFiles([]string{tc.dir})
+ if err != nil {
+ t.Fatal(err)
+ }
+ test.Assert(t, tc.dir, tc.exp, got)
}
- test.Assert(t, `scanFiles: testdata/scan/`, exp, got)
}
diff --git a/testdata/scan_no_scm/node_modules/dummy b/testdata/scan_no_scm/node_modules/dummy
new file mode 100644
index 0000000..4e16aa0
--- /dev/null
+++ b/testdata/scan_no_scm/node_modules/dummy
@@ -0,0 +1 @@
+Dummy content for node_modules directory. \ No newline at end of file
diff --git a/testdata/scan_no_scm/test.go b/testdata/scan_no_scm/test.go
new file mode 100644
index 0000000..46e2bd9
--- /dev/null
+++ b/testdata/scan_no_scm/test.go
@@ -0,0 +1,3 @@
+// Copyright 2022, Shulhan <ms@kilabit.info>. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file. \ No newline at end of file
diff --git a/testdata/scan_no_scm/test.html b/testdata/scan_no_scm/test.html
new file mode 100644
index 0000000..da2491e
--- /dev/null
+++ b/testdata/scan_no_scm/test.html
@@ -0,0 +1,5 @@
+<!--
+Copyright 2022, Shulhan <ms@kilabit.info>. All rights reserved.
+Use of this source code is governed by a BSD-style
+license that can be found in the LICENSE file.
+--> \ No newline at end of file
diff --git a/testdata/scan_no_scm/test.sh b/testdata/scan_no_scm/test.sh
new file mode 100644
index 0000000..8fd6949
--- /dev/null
+++ b/testdata/scan_no_scm/test.sh
@@ -0,0 +1,3 @@
+# Copyright 2022, Shulhan <ms@kilabit.info>. All rights reserved.
+# Use of this source code is governed by a BSD-style
+# license that can be found in the LICENSE file. \ No newline at end of file
diff --git a/testdata/scan_no_scm/vendor/dummy b/testdata/scan_no_scm/vendor/dummy
new file mode 100644
index 0000000..b244b09
--- /dev/null
+++ b/testdata/scan_no_scm/vendor/dummy
@@ -0,0 +1 @@
+Dummy content for vendor directory. \ No newline at end of file