-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move reading of input to utils package (#18)
- Loading branch information
1 parent
40228c9
commit 99463ed
Showing
4 changed files
with
94 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package utils | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
// GetUserInput will open and read from the given filename. If filename is | ||
// "-", it will read from the given file instead. | ||
func GetUserInput(filename string, file io.Reader) (b []byte, err error) { | ||
if filename != "-" { | ||
file, err = os.Open(filename) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not open file: %w", err) | ||
} | ||
} | ||
|
||
b, err = io.ReadAll(file) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not read file: %w", err) | ||
} | ||
|
||
if len(b) == 0 { | ||
return nil, errors.New("no input given") | ||
} | ||
|
||
return b, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetUserInput_File(t *testing.T) { | ||
in := []byte("foo") | ||
f := writeToTempFile(t, in) | ||
|
||
b, err := GetUserInput(f.Name(), nil) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, in, b) | ||
} | ||
|
||
func TestGetUserInput_BadFile(t *testing.T) { | ||
b, err := GetUserInput("notafile", nil) | ||
|
||
assert.Nil(t, b) | ||
assert.ErrorContains(t, err, "could not open file") | ||
} | ||
|
||
func TestGetUserInput_Stdin(t *testing.T) { | ||
in := []byte("bar") | ||
|
||
b, err := GetUserInput("-", bytes.NewReader(in)) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, in, b) | ||
} | ||
|
||
func TestGetUserInput_NoContent(t *testing.T) { | ||
in := new([]byte) | ||
|
||
b, err := GetUserInput("-", bytes.NewReader(*in)) | ||
|
||
assert.ErrorContains(t, err, "no input given") | ||
assert.Nil(t, b) | ||
} | ||
|
||
func writeToTempFile(t *testing.T, b []byte) *os.File { | ||
t.Helper() | ||
|
||
f, err := os.CreateTemp("", "tmpfile-") | ||
require.NoError(t, err) | ||
|
||
n, err := f.Write(b) | ||
require.Equal(t, len(b), n) | ||
require.NoError(t, err) | ||
|
||
return f | ||
} |