From c7744a9b8badcac26a0b9ef18fc6cae390f7b19e Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sat, 12 Mar 2022 01:10:39 +0700 Subject: 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. --- ciigo.go | 7 +++--- ciigo_test.go | 56 ++++++++++++++++++++++++++++++++++++++++++++ testdata/ex/clu/de/file.adoc | 1 + testdata/in/clu/de/file.adoc | 1 + 4 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 ciigo_test.go create mode 100644 testdata/ex/clu/de/file.adoc create mode 100644 testdata/in/clu/de/file.adoc 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 +// 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 -- cgit v1.3