Skip to content

Commit 2b2804c

Browse files
committed
Added test and rewrote the whole thing basically
1 parent ffcbd35 commit 2b2804c

File tree

6 files changed

+60
-0
lines changed

6 files changed

+60
-0
lines changed

internal/stage4.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"strings"
77

8+
"github.com/codecrafters-io/shell-tester/internal/condition_reader"
89
"github.com/codecrafters-io/shell-tester/internal/logged_shell_asserter"
910
"github.com/codecrafters-io/shell-tester/internal/shell_executable"
1011
"github.com/codecrafters-io/shell-tester/internal/test_cases"
@@ -43,9 +44,14 @@ func testExit(stageHarness *test_case_harness.TestCaseHarness) error {
4344
readErr := shell.ReadUntilConditionOrTimeout(utils.AsBool(assertFn), logged_shell_asserter.SUBSEQUENT_READ_TIMEOUT)
4445
output := virtual_terminal.BuildCleanedRow(shell.GetScreenState()[asserter.GetLastLoggedRowIndex()+1])
4546

47+
asserter.LogRemainingOutput()
48+
4649
// We're expecting EOF since the program should've terminated
4750
if !errors.Is(readErr, shell_executable.ErrProgramExited) {
4851
if readErr == nil {
52+
return fmt.Errorf("Expected program to exit with 0 exit code, program is still running.")
53+
} else if errors.Is(readErr, condition_reader.ErrConditionNotMet) {
54+
4955
return fmt.Errorf("Expected program to exit with 0 exit code, program is still running.")
5056
} else {
5157
// TODO: Other than ErrProgramExited, what other errors could we get? Are they user errors or internal errors?

internal/stages_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ func TestStages(t *testing.T) {
3333
StdoutFixturePath: "./test_helpers/fixtures/escape_codes",
3434
NormalizeOutputFunc: normalizeTesterOutput,
3535
},
36+
"exit_error_fail": {
37+
UntilStageSlug: "pn5",
38+
CodePath: "./test_helpers/scenarios/exit_error",
39+
ExpectedExitCode: 1,
40+
StdoutFixturePath: "./test_helpers/fixtures/exit_error",
41+
NormalizeOutputFunc: normalizeTesterOutput,
42+
},
3643
"base_pass_bash": {
3744
UntilStageSlug: "ip1",
3845
CodePath: "./test_helpers/bash",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Debug = true
2+
3+
[stage-4] Running tests for Stage #4: pn5
4+
[stage-4] Running ./your_shell.sh
5+
[your-program] $ invalid_apple_command
6+
[your-program] invalid_apple_command: command not found
7+
[stage-4] ✓ Received command not found message
8+
[your-program] $ exit 0
9+
[your-program] exit: command not found
10+
[your-program] $
11+
[stage-4] Expected program to exit with 0 exit code, program is still running.
12+
[stage-4] Test failed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Set this to true if you want debug logs.
2+
#
3+
# These can be VERY verbose, so we suggest turning them off
4+
# unless you really need them.
5+
debug: true
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import sys
2+
3+
4+
def main():
5+
while True:
6+
sys.stdout.write("$ ")
7+
sys.stdout.flush()
8+
9+
command = input().strip()
10+
parts = command.split(" ")
11+
cmd = parts[0]
12+
args = parts[1:]
13+
14+
if cmd == "exitt":
15+
sys.exit(0)
16+
else:
17+
sys.stdout.write(f"{cmd}: command not found\n")
18+
sys.stdout.flush()
19+
20+
21+
if __name__ == "__main__":
22+
main()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
#
3+
# DON'T EDIT THIS!
4+
#
5+
# CodeCrafters uses this file to test your code. Don't make any changes here!
6+
#
7+
# DON'T EDIT THIS!
8+
exec python3 $(dirname "$0")/main.py "$@"

0 commit comments

Comments
 (0)