Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CC-1318 Add support for the --previous flag for the test command #33

Merged
merged 8 commits into from
Jul 3, 2024
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious - what happens if a user makes a typo here? If they do codecrafters test --previos for example? Will this complain? (Ideally it should)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2024-06-25 at 6 37 02 PM

tried it out and it does complain if a nonexistent flag is used for test


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
Loading