Skip to content

Commit 4e2c31d

Browse files
authored
Merge pull request #33 from codecrafters-io/add-support-for-previous
CC-1318 Add support for the --previous flag for the test command
2 parents d9d7c00 + 1f312ce commit 4e2c31d

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

cmd/codecrafters/main.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ USAGE
2323
$ codecrafters [command]
2424
2525
EXAMPLES
26-
$ codecrafters test # Run tests without committing changes
27-
$ codecrafters submit # Commit changes & submit to move to next step
26+
$ codecrafters test # Run tests without committing changes
27+
$ codecrafters test --previous # Run tests for the current stage and all previous stages without committing changes
28+
$ codecrafters submit # Commit changes & submit to move to next step
2829
2930
COMMANDS
3031
test: Run tests without committing changes
@@ -76,7 +77,11 @@ func run() error {
7677

7778
switch cmd {
7879
case "test":
79-
return commands.TestCommand(ctx)
80+
testCmd := flag.NewFlagSet("test", flag.ExitOnError)
81+
shouldTestPrevious := testCmd.Bool("previous", false, "run tests for the current stage and all previous stages in ascending order")
82+
testCmd.Parse(flag.Args()[1:]) // parse the args after the test command
83+
84+
return commands.TestCommand(ctx, *shouldTestPrevious)
8085
case "submit":
8186
return commands.SubmitCommand(ctx)
8287
case "help",

internal/commands/submit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func SubmitCommand(ctx context.Context) (err error) {
9191

9292
logger.Debug().Msgf("creating submission for %s", commitSha)
9393

94-
createSubmissionResponse, err := codecraftersClient.CreateSubmission(codecraftersRemote.CodecraftersRepositoryId(), commitSha, "submit")
94+
createSubmissionResponse, err := codecraftersClient.CreateSubmission(codecraftersRemote.CodecraftersRepositoryId(), commitSha, "submit", "current_and_previous_descending")
9595
if err != nil {
9696
return fmt.Errorf("create submission: %w", err)
9797
}

internal/commands/test.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/rs/zerolog"
1818
)
1919

20-
func TestCommand(ctx context.Context) (err error) {
20+
func TestCommand(ctx context.Context, shouldTestPrevious bool) (err error) {
2121
logger := zerolog.Ctx(ctx)
2222

2323
logger.Debug().Msg("test command starts")
@@ -105,7 +105,13 @@ func TestCommand(ctx context.Context) (err error) {
105105

106106
logger.Debug().Msgf("creating submission for %s", tempCommitSha)
107107

108-
createSubmissionResponse, err := codecraftersClient.CreateSubmission(codecraftersRemote.CodecraftersRepositoryId(), tempCommitSha, "test")
108+
stageSelectionStrategy := "current_and_previous_descending"
109+
110+
if shouldTestPrevious {
111+
stageSelectionStrategy = "current_and_previous_ascending"
112+
}
113+
114+
createSubmissionResponse, err := codecraftersClient.CreateSubmission(codecraftersRemote.CodecraftersRepositoryId(), tempCommitSha, "test", stageSelectionStrategy)
109115
if err != nil {
110116
return fmt.Errorf("create submission: %w", err)
111117
}

internal/utils/codecrafters_client.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,13 @@ func (c CodecraftersClient) headers() map[string]string {
8888
}
8989
}
9090

91-
func (c CodecraftersClient) CreateSubmission(repositoryId string, commitSha string, command string) (CreateSubmissionResponse, error) {
91+
func (c CodecraftersClient) CreateSubmission(repositoryId string, commitSha string, command string, stageSelectionStrategy string) (CreateSubmissionResponse, error) {
9292
response, err := grequests.Post(c.ServerUrl+"/services/cli/create_submission", &grequests.RequestOptions{
9393
JSON: map[string]interface{}{
94-
"repository_id": repositoryId,
95-
"commit_sha": commitSha,
96-
"command": command,
94+
"repository_id": repositoryId,
95+
"commit_sha": commitSha,
96+
"command": command,
97+
"stage_selection_strategy": stageSelectionStrategy,
9798
},
9899
Headers: c.headers(),
99100
})

0 commit comments

Comments
 (0)