Skip to content

Commit

Permalink
Merge pull request #33 from codecrafters-io/add-support-for-previous
Browse files Browse the repository at this point in the history
CC-1318 Add support for the --previous flag for the test command
  • Loading branch information
libmartinito authored Jul 3, 2024
2 parents d9d7c00 + 1f312ce commit 4e2c31d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
11 changes: 8 additions & 3 deletions cmd/codecrafters/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
10 changes: 8 additions & 2 deletions internal/commands/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 5 additions & 4 deletions internal/utils/codecrafters_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
Expand Down

0 comments on commit 4e2c31d

Please sign in to comment.