Skip to content

Commit 457b0c3

Browse files
committed
Merge branch 'main' into arpan/cc-1840-figure-out-test-failure-for-shell-challenge
2 parents e5277d9 + b995906 commit 457b0c3

File tree

26 files changed

+157
-115
lines changed

26 files changed

+157
-115
lines changed

built_executables/wc_darwin_amd64

-8.75 KB
Binary file not shown.

built_executables/wc_darwin_arm64

-768 Bytes
Binary file not shown.

built_executables/wc_linux_amd64

-11 KB
Binary file not shown.

built_executables/wc_linux_arm64

-2.22 KB
Binary file not shown.

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ toolchain go1.24.2
66

77
require (
88
github.com/charmbracelet/x/vt v0.0.0-20250122132629-a969ddeb820d
9-
github.com/codecrafters-io/tester-utils v0.4.2
9+
github.com/codecrafters-io/tester-utils v0.4.5
1010
github.com/creack/pty v1.1.24
1111
github.com/fatih/color v1.18.0
1212
go.chromium.org/luci v0.0.0-20250611085002-a5741c865576
@@ -30,7 +30,7 @@ require (
3030
github.com/rogpeppe/go-internal v1.14.1 // indirect
3131
github.com/stretchr/testify v1.10.0 // indirect
3232
golang.org/x/exp v0.0.0-20250606033433-dcc06ee1d476 // indirect
33-
golang.org/x/sys v0.33.0 // indirect
33+
golang.org/x/sys v0.34.0 // indirect
3434
golang.org/x/term v0.32.0 // indirect
3535
golang.org/x/text v0.24.0 // indirect
3636
google.golang.org/protobuf v1.36.6 // indirect

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ github.com/charmbracelet/x/vt v0.0.0-20250122132629-a969ddeb820d h1:cIxw4y+aEGux
44
github.com/charmbracelet/x/vt v0.0.0-20250122132629-a969ddeb820d/go.mod h1:RB6f81fnGXyFGRenuO1fMdTlBh0Dbp/0+jwiMtgZvJw=
55
github.com/charmbracelet/x/wcwidth v0.0.0-20241011142426-46044092ad91 h1:D5OO0lVavz7A+Swdhp62F9gbkibxmz9B2hZ/jVdMPf0=
66
github.com/charmbracelet/x/wcwidth v0.0.0-20241011142426-46044092ad91/go.mod h1:Ey8PFmYwH+/td9bpiEx07Fdx9ZVkxfIjWXxBluxF4Nw=
7-
github.com/codecrafters-io/tester-utils v0.4.2 h1:WwSq/2uEQDPRQuTZtjeswwbzqNoI9cNYPTAY6qYRA4Q=
8-
github.com/codecrafters-io/tester-utils v0.4.2/go.mod h1:Fyrv4IebzjWtvKfpYf8ooYDoOtjYe2qx8bV7KAJpX+w=
7+
github.com/codecrafters-io/tester-utils v0.4.5 h1:iSyDAICLI7E8FxMwz0eMRvxCd14P7vfr0KtL8Eh5F8w=
8+
github.com/codecrafters-io/tester-utils v0.4.5/go.mod h1:Fyrv4IebzjWtvKfpYf8ooYDoOtjYe2qx8bV7KAJpX+w=
99
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
1010
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
1111
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
@@ -47,8 +47,8 @@ go.chromium.org/luci v0.0.0-20250611085002-a5741c865576/go.mod h1:Sq6fyDyzMSLbIc
4747
golang.org/x/exp v0.0.0-20250606033433-dcc06ee1d476 h1:bsqhLWFR6G6xiQcb+JoGqdKdRU6WzPWmK8E0jxTjzo4=
4848
golang.org/x/exp v0.0.0-20250606033433-dcc06ee1d476/go.mod h1:3//PLf8L/X+8b4vuAfHzxeRUl04Adcb341+IGKfnqS8=
4949
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
50-
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
51-
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
50+
golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=
51+
golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
5252
golang.org/x/term v0.32.0 h1:DR4lr0TjUs3epypdhTOkMmuF5CDFJ/8pOnbzMZPQ7bg=
5353
golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ=
5454
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=

internal/custom_executable/wc/wc.go

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"bufio"
54
"errors"
65
"flag"
76
"fmt"
@@ -10,16 +9,6 @@ import (
109
"strings"
1110
)
1211

13-
// byteCounter implements io.Writer to count bytes written to it.
14-
type byteCounter struct {
15-
count int64
16-
}
17-
18-
func (bc *byteCounter) Write(p []byte) (int, error) {
19-
bc.count += int64(len(p))
20-
return len(p), nil
21-
}
22-
2312
// counts holds the counts for lines, words, and bytes.
2413
type counts struct {
2514
lines, words, bytes int64
@@ -121,38 +110,22 @@ func main() {
121110
// countReader now accepts flags to potentially optimize (e.g., skip word count if !wFlag)
122111
func countReader(r io.Reader, countLines, countWords, countBytes bool) (counts, error) {
123112
var c counts
124-
var readerForScanner = r // Start with the original reader
125113

126-
// Setup byte counter only if needed
127-
var counter *byteCounter
128-
if countBytes {
129-
counter = &byteCounter{}
130-
// If we need bytes, tee the original reader
131-
readerForScanner = io.TeeReader(r, counter)
114+
data, err := io.ReadAll(r)
115+
if err != nil {
116+
return counts{}, err
132117
}
133118

134-
// Scanner reads from the (potentially tee'd) reader
135-
scanner := bufio.NewScanner(readerForScanner)
136-
137-
for scanner.Scan() {
138-
// Always count lines if requested or if words are needed (scanner works line by line)
139-
if countLines || countWords {
140-
c.lines++
141-
}
142-
// Only count words if requested
143-
if countWords {
144-
c.words += int64(len(strings.Fields(scanner.Text())))
145-
}
146-
// Byte counting happens via the TeeReader automatically if countBytes is true
119+
if countLines {
120+
c.lines = int64(strings.Count(string(data), "\n"))
147121
}
148122

149-
if err := scanner.Err(); err != nil {
150-
return counts{}, err // Return zero counts and the error
123+
if countWords {
124+
c.words = int64(len(strings.Fields(string(data))))
151125
}
152126

153-
// Assign byte count if it was calculated
154-
if countBytes && counter != nil {
155-
c.bytes = counter.count
127+
if countBytes {
128+
c.bytes = int64(len(data))
156129
}
157130

158131
return c, nil

internal/custom_executable/wc/wc_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,43 @@ func TestWcWithPipedInput(t *testing.T) {
418418
}
419419
}
420420

421+
func TestWcFileWithoutTrailingNewline(t *testing.T) {
422+
tmpDir, err := os.MkdirTemp("", "wc-test-*")
423+
if err != nil {
424+
t.Fatal(err)
425+
}
426+
currentDir, err := os.Getwd()
427+
if err != nil {
428+
t.Fatal(err)
429+
}
430+
if err := os.Chdir(tmpDir); err != nil {
431+
t.Fatal(err)
432+
}
433+
defer os.Chdir(currentDir)
434+
defer cleanupDirectories([]string{tmpDir})
435+
436+
// Content with 5 lines but only 4 newlines (no trailing newline)
437+
content := "First line\nSecond line\nThird line\nFourth line\nFifth line"
438+
testFiles := []testFile{
439+
{name: "no_trailing_newline.txt", content: []byte(content), mode: 0644},
440+
}
441+
createTestFiles(t, tmpDir, testFiles)
442+
443+
output, exitCode, err := runWc(t, "no_trailing_newline.txt")
444+
if err != nil {
445+
t.Fatalf("Expected no error, got: %v", err)
446+
}
447+
448+
expected := " 4 10 56 no_trailing_newline.txt\n"
449+
if output != expected {
450+
t.Errorf("Expected output %q, got %q", expected, output)
451+
}
452+
453+
if exitCode != 0 {
454+
t.Errorf("Expected exit code 0, got %d", exitCode)
455+
}
456+
}
457+
421458
func cleanupDirectories(dirs []string) {
422459
for _, dir := range dirs {
423460
if err := os.RemoveAll(dir); err != nil {

internal/stage7.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ func logPath(shell *shell_executable.ShellExecutable, logger *logger.Logger, pre
113113
}
114114
}
115115

116-
logger.UpdateSecondaryPrefix("setup")
116+
logger.UpdateLastSecondaryPrefix("setup")
117117
logger.Infof("PATH is now: %s", path)
118-
logger.ResetSecondaryPrefix()
118+
logger.ResetSecondaryPrefixes()
119119
}
120120

121121
func setUpNonExecutable(stageHarness *test_case_harness.TestCaseHarness, shell *shell_executable.ShellExecutable, commandName string) (string, error) {

internal/stage_a6.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ func testA6(stageHarness *test_case_harness.TestCaseHarness) error {
5252

5353
// TODO: Think of how to encapsulate this inside SetupExecutable function
5454
func logAvailableExecutables(logger *logger.Logger, executableNames []string) {
55-
logger.UpdateSecondaryPrefix("setup")
55+
logger.UpdateLastSecondaryPrefix("setup")
5656
logger.Infof("Available executables:")
5757
for _, executableName := range executableNames {
5858
logger.Infof("- %s", executableName)
5959
}
60-
logger.ResetSecondaryPrefix()
60+
logger.ResetSecondaryPrefixes()
6161
}

0 commit comments

Comments
 (0)