From b84dee88ce2c491f868b6386bcfefab7995496a9 Mon Sep 17 00:00:00 2001 From: Christian Date: Sat, 14 Dec 2024 14:58:58 -0800 Subject: [PATCH 1/4] updating version numbers --- .github/workflows/ci.yml | 3 ++- README.md | 46 +++++++++++++++++++++------------------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8d97b43..40b8cb3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,8 +23,9 @@ on: - '.goreleaser.yaml' branches: - main + env: - VERSION_NUMBER: 'v0.9.1' + VERSION_NUMBER: 'v0.9.2' DOCKERHUB_REGISTRY_NAME: 'digitalghostdev/poke-cli' AWS_REGION: 'us-west-2' diff --git a/README.md b/README.md index 9ab8d14..6edd02d 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,13 @@ pokemon-logo

Pokémon CLI

version-label - docker-image-size + docker-image-size ci-status-badge
tests-label - go-version - + go-version + codecov
## Overview @@ -68,7 +68,7 @@ _Use a Docker Image_ * Necessary. ```bash -docker run --rm -i -t digitalghostdev/poke-cli:v0.9.1 [subcommand] flag] +docker run --rm -i -t digitalghostdev/poke-cli:v0.9.2 [subcommand] flag] ``` ### Go Install @@ -83,23 +83,23 @@ _If you have Go already, install the executable yourself_ ## Usage By running `poke-cli [-h | --help]`, it'll display information on how to use the tool. ``` -╭──────────────────────────────────────────────────────╮ -│Welcome! This tool displays data related to Pokémon! │ -│ │ -│ USAGE: │ -│ poke-cli [flag] │ -│ poke-cli [flag] │ -│ poke-cli [flag] │ -│ │ -│ FLAGS: │ -│ -h, --help Shows the help menu │ -│ -l, --latest Prints the latest available │ -│ version of the program │ -│ │ -│ AVAILABLE COMMANDS: │ -│ pokemon Get details of a specific Pokémon │ -│ types Get details of a specific typing │ -╰──────────────────────────────────────────────────────╯ +╭────────────────────────────────────────────────────────╮ +│Welcome! This tool displays data related to Pokémon! │ +│ │ +│ USAGE: │ +│ poke-cli [flag] │ +│ poke-cli [flag] │ +│ poke-cli [flag] │ +│ │ +│ FLAGS: │ +│ -h, --help Shows the help menu │ +│ -l, --latest Prints the latest version available │ +│ -v, --version Prints the current version │ +│ │ +│ AVAILABLE COMMANDS: │ +│ pokemon Get details of a specific Pokémon │ +│ types Get details of a specific typing │ +╰────────────────────────────────────────────────────────╯ ``` --- @@ -119,4 +119,6 @@ _Not 100% up-to-date, may add or remove some of these choices_ - [ ] `-m | --moves`: display learnable moves. - [x] `types`: get data about a specific typing. - [ ] `ability`: get data about a specific ability. -- [ ] `move`: get data about a specific move. \ No newline at end of file + - [ ] `-p | --pokemon`: display Pokémon that learn this ability. +- [ ] `move`: get data about a specific move. + - [ ] `-p | --pokemon`: display Pokémon that learn this move. \ No newline at end of file From 1a04fbd58d6cdcf91beeadb0b8f67eb41418e1e7 Mon Sep 17 00:00:00 2001 From: Christian Date: Sat, 14 Dec 2024 15:01:05 -0800 Subject: [PATCH 2/4] updating tests --- cli_test.go | 100 ++++++++++++++++++++++++++++++++++- flags/pokemonflagset_test.go | 8 +-- 2 files changed, 103 insertions(+), 5 deletions(-) diff --git a/cli_test.go b/cli_test.go index f8d5801..6638c60 100644 --- a/cli_test.go +++ b/cli_test.go @@ -7,6 +7,63 @@ import ( "testing" ) +func TestCurrentVersion(t *testing.T) { + tests := []struct { + name string + version string + expectedOutput string + }{ + { + name: "Version set by ldflags", + version: "v1.0.0", + expectedOutput: "Version: v1.0.0\n", + }, + { + name: "Version set to (devel)", + version: "(devel)", + expectedOutput: "Version: (devel)\n", // Simplified assumption + }, + } + + // Save the original version and restore it after tests + originalVersion := version + defer func() { version = originalVersion }() + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Set the version for this test case + version = tt.version + + // Redirect stdout to capture the output + r, w, _ := os.Pipe() + oldStdout := os.Stdout + os.Stdout = w + + // Call the function + currentVersion() + + // Close the writer and restore stdout + err := w.Close() + if err != nil { + t.Fatalf("Failed to close pipe: %v", err) + } + os.Stdout = oldStdout + + // Read the output from the pipe + var buf bytes.Buffer + if _, err := buf.ReadFrom(r); err != nil { + t.Fatalf("Failed to read from pipe: %v", err) + } + + // Compare the output with the expected result + got := buf.String() + if got != tt.expectedOutput { + t.Errorf("Expected %q, got %q", tt.expectedOutput, got) + } + }) + } +} + var ansiRegex = regexp.MustCompile(`\x1b\[[0-9;]*m`) func captureOutput(f func()) string { @@ -110,7 +167,7 @@ func TestRunCLI(t *testing.T) { { name: "Latest Flag", args: []string{"-l"}, - expectedOutput: "Latest Docker image version: v0.9.0\nLatest release tag: v0.9.0\n", + expectedOutput: "Latest Docker image version: v0.9.1\nLatest release tag: v0.9.1\n", expectedCode: 0, }, } @@ -133,3 +190,44 @@ func TestRunCLI(t *testing.T) { }) } } + +func TestMainFunction(t *testing.T) { + originalExit := exit + defer func() { exit = originalExit }() // Restore original exit after test + + tests := []struct { + name string + args []string + expectedOutput string + expectedCode int + }{ + { + name: "Run main command", + args: []string{"poke-cli"}, + expectedOutput: "Welcome! This tool displays data related to Pokémon!", + expectedCode: 0, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + exitCode := 0 + exit = func(code int) { exitCode = code } + + output := captureOutput(func() { + os.Args = tt.args + main() + }) + + output = stripANSI(output) + + if exitCode != tt.expectedCode { + t.Errorf("Expected exit code %d, got %d", tt.expectedCode, exitCode) + } + + if !bytes.Contains([]byte(output), []byte(tt.expectedOutput)) { + t.Errorf("Expected output to contain %q, got %q", tt.expectedOutput, output) + } + }) + } +} diff --git a/flags/pokemonflagset_test.go b/flags/pokemonflagset_test.go index 8fd797e..9363f9a 100644 --- a/flags/pokemonflagset_test.go +++ b/flags/pokemonflagset_test.go @@ -74,8 +74,8 @@ func TestAbilitiesFlag(t *testing.T) { // Define the expected output based on the API response expectedOutput := `───────── Abilities -Ability 1: overgrow -Hidden Ability: chlorophyll +Ability 1: Overgrow +Hidden Ability: Chlorophyll ` // Assert the actual output matches the expected output @@ -153,8 +153,8 @@ func TestTypesFlag(t *testing.T) { // Define expected output components expectedOutput := `────── Typing -Type 1: grass -Type 2: poison +Type 1: Grass +Type 2: Poison ` // Assert output contains the expected header and typing information From 8bb9c55d938a3b83e43cba575b89bde21ef337e4 Mon Sep 17 00:00:00 2001 From: Christian Date: Sat, 14 Dec 2024 15:02:45 -0800 Subject: [PATCH 3/4] editing comment --- cli.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli.go b/cli.go index bfccc98..f0bcd8b 100644 --- a/cli.go +++ b/cli.go @@ -51,7 +51,7 @@ func runCLI(args []string) int { latestFlag := mainFlagSet.Bool("latest", false, "Prints the program's latest Docker image and release versions.") shortLatestFlag := mainFlagSet.Bool("l", false, "Prints the program's latest Docker image and release versions.") - // -v, --version flag retrives the currently installed version + // -v, --version flag retrieves the currently installed version currentVersionFlag := mainFlagSet.Bool("version", false, "Prints the current version") shortCurrentVersionFlag := mainFlagSet.Bool("v", false, "Prints the current version") From db1ec6eb2fdec4dd8ac1d7ce2433fa06d9593479 Mon Sep 17 00:00:00 2001 From: Christian Date: Sat, 14 Dec 2024 15:04:02 -0800 Subject: [PATCH 4/4] fixing string formatting (#85) --- cmd/pokemon.go | 2 +- flags/pokemonflagset.go | 39 ++++++++++++++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/cmd/pokemon.go b/cmd/pokemon.go index 32f56ee..ee8ae6b 100644 --- a/cmd/pokemon.go +++ b/cmd/pokemon.go @@ -53,7 +53,7 @@ func PokemonCommand() { } _, pokemonName, pokemonID, pokemonWeight, pokemonHeight := connections.PokemonApiCall(endpoint, pokemonName, "https://pokeapi.co/api/v2/") - capitalizedString := cases.Title(language.English).String(pokemonName) + capitalizedString := cases.Title(language.English).String(strings.Replace(pokemonName, "-", " ", -1)) // Weight calculation weightKilograms := float64(pokemonWeight) / 10 diff --git a/flags/pokemonflagset.go b/flags/pokemonflagset.go index 81550f8..fea185e 100644 --- a/flags/pokemonflagset.go +++ b/flags/pokemonflagset.go @@ -7,6 +7,8 @@ import ( "fmt" "github.com/charmbracelet/lipgloss" "github.com/digitalghost-dev/poke-cli/connections" + "golang.org/x/text/cases" + "golang.org/x/text/language" "strings" ) @@ -58,13 +60,38 @@ func AbilitiesFlag(endpoint string, pokemonName string) error { header("Abilities") + // Anonymous function to format ability names + formatAbilityName := func(name string) string { + exceptions := map[string]bool{ + "of": true, + "the": true, + "to": true, + "as": true, + } + + name = strings.Replace(name, "-", " ", -1) + words := strings.Split(name, " ") + titleCaser := cases.Title(language.English) + + // Process each word + for i, word := range words { + if _, found := exceptions[strings.ToLower(word)]; found && i != 0 { + words[i] = strings.ToLower(word) + } else { + words[i] = titleCaser.String(word) + } + } + return strings.Join(words, " ") + } + for _, pokeAbility := range pokemonStruct.Abilities { + formattedName := formatAbilityName(pokeAbility.Ability.Name) if pokeAbility.Slot == 1 { - fmt.Printf("Ability %d: %s\n", pokeAbility.Slot, pokeAbility.Ability.Name) + fmt.Printf("Ability %d: %s\n", pokeAbility.Slot, formattedName) } else if pokeAbility.Slot == 2 { - fmt.Printf("Ability %d: %s\n", pokeAbility.Slot, pokeAbility.Ability.Name) + fmt.Printf("Ability %d: %s\n", pokeAbility.Slot, formattedName) } else { - fmt.Printf("Hidden Ability: %s\n", pokeAbility.Ability.Name) + fmt.Printf("Hidden Ability: %s\n", formattedName) } } @@ -181,9 +208,11 @@ func TypesFlag(endpoint string, pokemonName string) error { colorHex, exists := colorMap[pokeType.Type.Name] if exists { color := lipgloss.Color(colorHex) - fmt.Printf("Type %d: %s\n", pokeType.Slot, lipgloss.NewStyle().Bold(true).Foreground(color).Render(pokeType.Type.Name)) + style := lipgloss.NewStyle().Bold(true).Foreground(color) + styledName := style.Render(cases.Title(language.English).String(pokeType.Type.Name)) // Apply styling here + fmt.Printf("Type %d: %s\n", pokeType.Slot, styledName) // Interpolate styled text } else { - fmt.Printf("Type %d: %s\n", pokeType.Slot, pokeType.Type.Name) + fmt.Printf("Type %d: %s\n", pokeType.Slot, cases.Title(language.English).String(pokeType.Type.Name)) } }