Skip to content

Commit fe6a2b2

Browse files
authored
Merge pull request #167 from codecrafters-io/andy/fix
Update writeFiles function to handle newline characters more accurately
2 parents 6bb8f92 + f354218 commit fe6a2b2

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

internal/test_helpers/fixtures/ash/pipelines/pass

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ Debug = true
33
[tester::#BR6] Running tests for Stage #BR6 (br6)
44
[tester::#BR6] [setup] export PATH=/tmp/pear/orange/raspberry:$PATH
55
[tester::#BR6] Running ./your_shell.sh
6-
[tester::#BR6] [setup] echo "strawberry apple\npear banana\nraspberry mango\nblueberry pineapple\norange grape" > "/tmp/foo/file-24"
6+
[tester::#BR6] [setup] echo -e "strawberry apple\npear banana\nraspberry mango\nblueberry pineapple\norange grape" > "/tmp/foo/file-24"
77
[your-program] $ cat /tmp/foo/file-24 | wc
88
[your-program]  5 10 78
99
[tester::#BR6] ✓ Received expected response
10-
[tester::#BR6] [setup] echo "1. orange blueberry\n2. strawberry pineapple\n3. banana mango" > "/tmp/baz/file-3"
10+
[tester::#BR6] [setup] echo -e "1. orange blueberry\n2. strawberry pineapple\n3. banana mango" > "/tmp/baz/file-3"
1111
[your-program] $ tail -f /tmp/baz/file-3 | head -n 5
1212
[your-program] 1. orange blueberry
1313
[your-program] 2. strawberry pineapple
@@ -33,7 +33,7 @@ Debug = true
3333

3434
[tester::#XK3] Running tests for Stage #XK3 (xk3)
3535
[tester::#XK3] [setup] export PATH=/tmp/orange/pineapple/orange:$PATH
36-
[tester::#XK3] [setup] echo "pineapple\nraspberry\nblueberry\ngrape\norange" > "/tmp/quz/file-26"
36+
[tester::#XK3] [setup] echo -e "pineapple\nraspberry\nblueberry\ngrape\norange" > "/tmp/quz/file-26"
3737
[tester::#XK3] Running ./your_shell.sh
3838
[your-program] $ cat /tmp/quz/file-26 | head -n 5 | wc
3939
[your-program]  5 5 43

internal/test_helpers/fixtures/bash/pipelines/pass

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ Debug = true
33
[tester::#BR6] Running tests for Stage #BR6 (br6)
44
[tester::#BR6] [setup] export PATH=/tmp/pear/orange/raspberry:$PATH
55
[tester::#BR6] Running ./your_shell.sh
6-
[tester::#BR6] [setup] echo "strawberry apple\npear banana\nraspberry mango\nblueberry pineapple\norange grape" > "/tmp/foo/file-24"
6+
[tester::#BR6] [setup] echo -e "strawberry apple\npear banana\nraspberry mango\nblueberry pineapple\norange grape" > "/tmp/foo/file-24"
77
[your-program] $ cat /tmp/foo/file-24 | wc
88
[your-program]  5 10 78
99
[tester::#BR6] ✓ Received expected response
10-
[tester::#BR6] [setup] echo "1. orange blueberry\n2. strawberry pineapple\n3. banana mango" > "/tmp/baz/file-3"
10+
[tester::#BR6] [setup] echo -e "1. orange blueberry\n2. strawberry pineapple\n3. banana mango" > "/tmp/baz/file-3"
1111
[your-program] $ tail -f /tmp/baz/file-3 | head -n 5
1212
[your-program] 1. orange blueberry
1313
[your-program] 2. strawberry pineapple
@@ -33,7 +33,7 @@ Debug = true
3333

3434
[tester::#XK3] Running tests for Stage #XK3 (xk3)
3535
[tester::#XK3] [setup] export PATH=/tmp/orange/pineapple/orange:$PATH
36-
[tester::#XK3] [setup] echo "pineapple\nraspberry\nblueberry\ngrape\norange" > "/tmp/quz/file-26"
36+
[tester::#XK3] [setup] echo -e "pineapple\nraspberry\nblueberry\ngrape\norange" > "/tmp/quz/file-26"
3737
[tester::#XK3] Running ./your_shell.sh
3838
[your-program] $ cat /tmp/quz/file-26 | head -n 5 | wc
3939
[your-program]  5 5 43

internal/utils_fs.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,17 @@ func writeFiles(paths []string, contents []string, logger *logger.Logger) error
129129
logger.UpdateLastSecondaryPrefix("setup")
130130

131131
if strings.HasSuffix(content, "\n") {
132-
logger.Infof("echo %q > %q", content[:len(content)-1], paths[i])
132+
if strings.Count(content, "\n") == 1 { // Trailing newline only
133+
logger.Infof("echo %q > %q", content[:len(content)-1], paths[i])
134+
} else { // Newline(s) in the middle as well
135+
logger.Infof("echo -e %q > %q", content[:len(content)-1], paths[i])
136+
}
133137
} else {
134-
logger.Infof("echo -n %q > %q", content, paths[i])
138+
if strings.Contains(content, "\n") {
139+
logger.Infof("echo -ne %q > %q", content, paths[i])
140+
} else {
141+
logger.Infof("echo -n %q > %q", content, paths[i])
142+
}
135143
}
136144

137145
logger.ResetSecondaryPrefixes()

0 commit comments

Comments
 (0)