Skip to content

Commit

Permalink
Bug: fix running stdin (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
caitlinelfring authored Jun 20, 2021
1 parent e0c7d98 commit 0dc3fe3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
15 changes: 14 additions & 1 deletion pkg/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions pkg/util/contenttype.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions pkg/util/contenttype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

0 comments on commit 0dc3fe3

Please sign in to comment.