aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-03-12 01:10:39 +0700
committerShulhan <ms@kilabit.info>2022-03-12 01:10:39 +0700
commitc7744a9b8badcac26a0b9ef18fc6cae390f7b19e (patch)
treeeba624676a73677cc4afba1b6c66a9f27e47418c
parenta19849f5a835edd478edfd2b6cf5584426841ec1 (diff)
downloadciigo-c7744a9b8badcac26a0b9ef18fc6cae390f7b19e.tar.xz
all: check for excluded file before processing sub directory
Previously, if the file path match with one of the excluded pattern, we keep processing the sub directory to find the markup files. This may cause an error "too many open files" if excluded directory contains many sub directory and/or files. This changes fix this issue by checking the path with excluded pattern first before diving into sub directory.
-rw-r--r--ciigo.go7
-rw-r--r--ciigo_test.go56
-rw-r--r--testdata/ex/clu/de/file.adoc1
-rw-r--r--testdata/in/clu/de/file.adoc1
4 files changed, 62 insertions, 3 deletions
diff --git a/ciigo.go b/ciigo.go
index a6029cb..94e0f4a 100644
--- a/ciigo.go
+++ b/ciigo.go
@@ -291,6 +291,10 @@ func listFileMarkups(dir string, excRE []*regexp.Regexp) (
name := fi.Name()
filePath := filepath.Join(dir, name)
+ if isExcluded(filePath, excRE) {
+ continue
+ }
+
if fi.IsDir() {
if name[0] == '.' {
// Skip any directory start with '.'.
@@ -313,9 +317,6 @@ func listFileMarkups(dir string, excRE []*regexp.Regexp) (
if fi.Size() == 0 {
continue
}
- if isExcluded(filePath, excRE) {
- continue
- }
fmarkup, err := newFileMarkup(filePath, fi)
if err != nil {
return nil, fmt.Errorf("%s: %s: %w", logp, filePath, err)
diff --git a/ciigo_test.go b/ciigo_test.go
new file mode 100644
index 0000000..6754135
--- /dev/null
+++ b/ciigo_test.go
@@ -0,0 +1,56 @@
+// SPDX-FileCopyrightText: 2022 M. Shulhan <ms@kilabit.info>
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package ciigo
+
+import (
+ "regexp"
+ "sort"
+ "testing"
+
+ "github.com/shuLhan/share/lib/test"
+)
+
+func TestListFileMarkups(t *testing.T) {
+ var (
+ dir = "testdata"
+ )
+
+ cases := []struct {
+ excRegex string
+ exp []string
+ }{{
+ excRegex: `(ex)/.*`,
+ exp: []string{
+ "testdata/in/clu/de/file.adoc",
+ },
+ }, {
+ excRegex: `(in|ex)/.*`,
+ }, {
+ excRegex: `de`,
+ }, {
+ excRegex: `file$`,
+ exp: []string{
+ "testdata/ex/clu/de/file.adoc",
+ "testdata/in/clu/de/file.adoc",
+ },
+ }}
+
+ for _, c := range cases {
+ excre := regexp.MustCompile(c.excRegex)
+
+ list, err := listFileMarkups(dir, []*regexp.Regexp{excre})
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ got := make([]string, 0, len(list))
+ for k := range list {
+ got = append(got, k)
+ }
+
+ sort.Strings(got)
+
+ test.Assert(t, "list", c.exp, got)
+ }
+}
diff --git a/testdata/ex/clu/de/file.adoc b/testdata/ex/clu/de/file.adoc
new file mode 100644
index 0000000..1c09f40
--- /dev/null
+++ b/testdata/ex/clu/de/file.adoc
@@ -0,0 +1 @@
+= dummy
diff --git a/testdata/in/clu/de/file.adoc b/testdata/in/clu/de/file.adoc
new file mode 100644
index 0000000..1c09f40
--- /dev/null
+++ b/testdata/in/clu/de/file.adoc
@@ -0,0 +1 @@
+= dummy