Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions internal/count/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

// Result encodes the result of a counting operation on a file.
type Result struct {
Err error `json:"-"`
Name string `json:"name"`
Lines Lines `json:"lines"`
Bytes Bytes `json:"bytes"`
Expand Down Expand Up @@ -72,7 +73,7 @@
}

// One performs a counting operation on a single reader, returning the result and any error.
func One(in io.Reader, name string) (Result, error) {
func One(in io.Reader, name string) Result {
var (
lc Lines
bc Bytes
Expand All @@ -84,7 +85,7 @@

_, err := io.Copy(multi, in)
if err != nil {
return Result{}, err
return Result{Err: err}

Check warning on line 88 in internal/count/count.go

View check run for this annotation

Codecov / codecov/patch

internal/count/count.go#L88

Added line #L88 was not covered by tests
}

return Result{
Expand All @@ -93,12 +94,12 @@
Bytes: bc,
Words: wc,
Chars: cc,
}, nil
}
}

// All performs counting operations on multiple files concurrently
// gathering up the results and returning.
func All(files []string) (Results, error) {
func All(files []string) Results {
jobs := make(chan string)
counts := make(chan Result)

Expand Down Expand Up @@ -139,7 +140,7 @@
results = append(results, count)
}

return results, nil
return results
}

// worker is a concurrent worker contributing to counting in files,
Expand All @@ -151,7 +152,8 @@
for file := range files {
info, err := os.Stat(file)
if err != nil {
panic(err) // TODO: Not this
counts <- Result{Err: err}
return

Check warning on line 156 in internal/count/count.go

View check run for this annotation

Codecov / codecov/patch

internal/count/count.go#L155-L156

Added lines #L155 - L156 were not covered by tests
}
if info.IsDir() {
// Skip directories
Expand All @@ -160,17 +162,10 @@

f, err := os.Open(file)
if err != nil {
// If we can't open the file, just close it and move on
// TODO: Handle this better
f.Close()
continue
}
result, err := One(f, file)
if err != nil {
// Same as above
f.Close()
continue
counts <- Result{Err: err}
return

Check warning on line 166 in internal/count/count.go

View check run for this annotation

Codecov / codecov/patch

internal/count/count.go#L165-L166

Added lines #L165 - L166 were not covered by tests
}
result := One(f, file)
f.Close()
counts <- result
}
Expand Down
14 changes: 8 additions & 6 deletions internal/count/count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ var (

func TestCount(t *testing.T) {
tests := []struct {
name string
in io.Reader
name string
want count.Result
}{
{
Expand Down Expand Up @@ -105,9 +105,9 @@ func TestCount(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := count.One(tt.in, "")
got := count.One(tt.in, "")

test.Ok(t, err)
test.Ok(t, got.Err)
test.Equal(t, got, tt.want)
})
}
Expand All @@ -121,8 +121,10 @@ func TestCountAll(t *testing.T) {
filepath.Join("testdata", "TestCount", "dir"),
}

results, err := count.All(files)
test.Ok(t, err)
results := count.All(files)
for _, result := range results {
test.Ok(t, result.Err)
}

want := count.Results{
{
Expand Down Expand Up @@ -297,7 +299,7 @@ func BenchmarkCount(b *testing.B) {

b.ResetTimer()
for range b.N {
_, err := count.One(r, "bench")
_ = count.One(r, "bench")
if err != nil {
b.Fatalf("Count returned an error: %v", err)
}
Expand Down
40 changes: 26 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
"io"
"os"
"slices"
"time"

"github.com/FollowTheProcess/cli"
"github.com/FollowTheProcess/gowc/internal/count"
)

var (
version = "dev" // gowc version number, set at compile time by ldflags
commit = "unknown" // gowc commit hash, set at compile time with ldflags
date = "?" // gowc build date, set at compile time with ldflags
version = "dev" // gowc version number, set at compile time by ldflags
commit = "" // gowc commit hash, set at compile time with ldflags
date = "" // gowc build date, set at compile time with ldflags
)

func main() {
Expand Down Expand Up @@ -58,6 +59,7 @@

func doCount(options *countOptions) func(cmd *cli.Command, args []string) error {
return func(cmd *cli.Command, args []string) error {
start := time.Now()
stdout := cmd.Stdout()
switch len(args) {
case 0:
Expand All @@ -71,12 +73,13 @@
return fmt.Errorf("nothing to read from stdin")
}

result, err := count.One(fd, "stdin")
if err != nil {
result := count.One(fd, "stdin")
if result.Err != nil {
return result.Err
}
if err := result.Display(stdout, options.json); err != nil {

Check warning on line 80 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L76-L80

Added lines #L76 - L80 were not covered by tests
return err
}
return result.Display(stdout, options.json)

case 1:
// Read from the file
path := args[0]
Expand All @@ -86,23 +89,32 @@
}
defer file.Close()

result, err := count.One(file, path)
if err != nil {
result := count.One(file, path)
if result.Err != nil {
return result.Err
}

Check warning on line 95 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L94-L95

Added lines #L94 - L95 were not covered by tests
if err := result.Display(stdout, options.json); err != nil {
return err
}
return result.Display(stdout, options.json)

default:
// Count many files
results, err := count.All(args)
if err != nil {
return err
results := count.All(args)
for _, result := range results {
if result.Err != nil {
return result.Err
}

Check warning on line 106 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L105-L106

Added lines #L105 - L106 were not covered by tests
}
// Sort the results by name so it's deterministic
slices.SortFunc(results, cmpResult)

return results.Display(stdout, options.json)
if err := results.Display(stdout, options.json); err != nil {
return err
}

Check warning on line 113 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L112-L113

Added lines #L112 - L113 were not covered by tests
}

fmt.Fprintf(cmd.Stderr(), "\ntook %v\n", time.Since(start))
return nil
}
}

Expand Down
3 changes: 1 addition & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,14 @@ func TestCountFile(t *testing.T) {
func TestCountMany(t *testing.T) {
snap := snapshot.New(t, snapshot.Update(*update))

files := []string{
args := []string{
filepath.Join("internal", "count", "testdata", "TestCount", "moby_dick.txt"),
filepath.Join("internal", "count", "testdata", "TestCount", "another.txt"),
filepath.Join("internal", "count", "testdata", "TestCount", "onemore.txt"),
filepath.Join("internal", "count", "testdata", "TestCount", "dir"),
}
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
args := files

err := run(os.Stdin, stdout, stderr, args)
test.Ok(t, err)
Expand Down
Loading