diff --git a/pkg/parser/parser_test.go b/pkg/parser/parser_test.go index 4419a212..01c88296 100644 --- a/pkg/parser/parser_test.go +++ b/pkg/parser/parser_test.go @@ -27,7 +27,8 @@ func (p *testPrinter) Print(_ io.Writer, r *result.FileResults) error { } func testParser() *Parser { - return NewParser([]*rule.Rule{&rule.TestRule}, ignore.NewIgnore([]string{})) + r := rule.TestRule + return NewParser([]*rule.Rule{&r}, ignore.NewIgnore([]string{})) } func parsePathTests(t *testing.T) { @@ -77,6 +78,18 @@ func parsePathTests(t *testing.T) { assert.Len(t, pr.results, 0) assert.Equal(t, len(pr.results), violations) }) + + t.Run("violation in filename - empty file", func(t *testing.T) { + f, err := newFileWithPrefix(t, "whitelist", "") + assert.NoError(t, err) + + p := testParser() + pr := new(testPrinter) + violations := p.ParsePaths(pr, f.Name()) + assert.Len(t, pr.results, 1) + assert.Equal(t, len(pr.results), violations) + }) + t.Run("IsTextFileFromFilename failure", func(t *testing.T) { f, err := newFile(t, "") assert.NoError(t, err) diff --git a/pkg/util/contenttype.go b/pkg/util/contenttype.go index 039287f4..d388d295 100644 --- a/pkg/util/contenttype.go +++ b/pkg/util/contenttype.go @@ -33,6 +33,11 @@ func isTextFile(file *os.File) bool { // IsTextFileFromFilename returns an error if the filename is not of content-type 'text/*' func IsTextFileFromFilename(filename string) error { + // Don't check stdin to avoid closing it prematurely + if filename == os.Stdin.Name() { + return nil + } + f, err := os.Open(filename) if err != nil { return err diff --git a/pkg/util/contenttype_test.go b/pkg/util/contenttype_test.go index a16fbb87..52f1ad2b 100644 --- a/pkg/util/contenttype_test.go +++ b/pkg/util/contenttype_test.go @@ -93,4 +93,9 @@ func TestIsTextFileFromFilename(t *testing.T) { err := IsTextFileFromFilename("testdata") assert.EqualError(t, err, ErrIsDir.Error()) }) + + t.Run("stdin", func(t *testing.T) { + err := IsTextFileFromFilename(os.Stdin.Name()) + assert.NoError(t, err) + }) }