diff --git a/cmd/codecrafters/main.go b/cmd/codecrafters/main.go index ffc77b8..fffa3f0 100644 --- a/cmd/codecrafters/main.go +++ b/cmd/codecrafters/main.go @@ -23,8 +23,9 @@ USAGE $ codecrafters [command] EXAMPLES - $ codecrafters test # Run tests without committing changes - $ codecrafters submit # Commit changes & submit to move to next step + $ codecrafters test # Run tests without committing changes + $ codecrafters test --previous # Run tests for the current stage and all previous stages without committing changes + $ codecrafters submit # Commit changes & submit to move to next step COMMANDS test: Run tests without committing changes @@ -76,7 +77,11 @@ func run() error { switch cmd { case "test": - return commands.TestCommand(ctx) + testCmd := flag.NewFlagSet("test", flag.ExitOnError) + shouldTestPrevious := testCmd.Bool("previous", false, "run tests for the current stage and all previous stages in ascending order") + testCmd.Parse(flag.Args()[1:]) // parse the args after the test command + + return commands.TestCommand(ctx, *shouldTestPrevious) case "submit": return commands.SubmitCommand(ctx) case "help", diff --git a/internal/commands/submit.go b/internal/commands/submit.go index 9dc3840..c151944 100644 --- a/internal/commands/submit.go +++ b/internal/commands/submit.go @@ -91,7 +91,7 @@ func SubmitCommand(ctx context.Context) (err error) { logger.Debug().Msgf("creating submission for %s", commitSha) - createSubmissionResponse, err := codecraftersClient.CreateSubmission(codecraftersRemote.CodecraftersRepositoryId(), commitSha, "submit") + createSubmissionResponse, err := codecraftersClient.CreateSubmission(codecraftersRemote.CodecraftersRepositoryId(), commitSha, "submit", "current_and_previous_descending") if err != nil { return fmt.Errorf("create submission: %w", err) } diff --git a/internal/commands/test.go b/internal/commands/test.go index ac351bc..480c401 100644 --- a/internal/commands/test.go +++ b/internal/commands/test.go @@ -17,7 +17,7 @@ import ( "github.com/rs/zerolog" ) -func TestCommand(ctx context.Context) (err error) { +func TestCommand(ctx context.Context, shouldTestPrevious bool) (err error) { logger := zerolog.Ctx(ctx) logger.Debug().Msg("test command starts") @@ -105,7 +105,13 @@ func TestCommand(ctx context.Context) (err error) { logger.Debug().Msgf("creating submission for %s", tempCommitSha) - createSubmissionResponse, err := codecraftersClient.CreateSubmission(codecraftersRemote.CodecraftersRepositoryId(), tempCommitSha, "test") + stageSelectionStrategy := "current_and_previous_descending" + + if shouldTestPrevious { + stageSelectionStrategy = "current_and_previous_ascending" + } + + createSubmissionResponse, err := codecraftersClient.CreateSubmission(codecraftersRemote.CodecraftersRepositoryId(), tempCommitSha, "test", stageSelectionStrategy) if err != nil { return fmt.Errorf("create submission: %w", err) } diff --git a/internal/utils/codecrafters_client.go b/internal/utils/codecrafters_client.go index 4c016fc..145d822 100644 --- a/internal/utils/codecrafters_client.go +++ b/internal/utils/codecrafters_client.go @@ -88,12 +88,13 @@ func (c CodecraftersClient) headers() map[string]string { } } -func (c CodecraftersClient) CreateSubmission(repositoryId string, commitSha string, command string) (CreateSubmissionResponse, error) { +func (c CodecraftersClient) CreateSubmission(repositoryId string, commitSha string, command string, stageSelectionStrategy string) (CreateSubmissionResponse, error) { response, err := grequests.Post(c.ServerUrl+"/services/cli/create_submission", &grequests.RequestOptions{ JSON: map[string]interface{}{ - "repository_id": repositoryId, - "commit_sha": commitSha, - "command": command, + "repository_id": repositoryId, + "commit_sha": commitSha, + "command": command, + "stage_selection_strategy": stageSelectionStrategy, }, Headers: c.headers(), })