-
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests and display coverage info
- Loading branch information
Showing
12 changed files
with
107 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
coverage: | ||
precision: 2 | ||
round: nearest | ||
range: "75...100" | ||
status: | ||
project: | ||
default: | ||
target: "75%" | ||
threshold: "0%" | ||
patch: false | ||
changes: false | ||
comment: | ||
layout: "diff, files" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
build/ | ||
coverage.out | ||
instagram_data.zip | ||
instagram_data/ | ||
build/ | ||
instagram_data/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package root | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"slices" | ||
"sort" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/cecobask/instagram-insights/cmd/followdata" | ||
"github.com/cecobask/instagram-insights/cmd/information" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func TestNewRootCommand(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
command *cobra.Command | ||
validationFunc func(*cobra.Command) error | ||
}{ | ||
{ | ||
name: "creates root command with default configuration", | ||
command: NewRootCommand(), | ||
validationFunc: func(cmd *cobra.Command) error { | ||
validSubcommands := []string{ | ||
followdata.CommandNameRoot, | ||
information.CommandNameRoot, | ||
} | ||
sort.Strings(validSubcommands) | ||
commands := cmd.Commands() | ||
if len(commands) != len(validSubcommands) { | ||
return fmt.Errorf("should have exactly %d subcommands", len(validSubcommands)) | ||
} | ||
equalFunc := func(a string, b *cobra.Command) bool { return a == b.Name() } | ||
if !slices.EqualFunc(validSubcommands, commands, equalFunc) { | ||
return fmt.Errorf("should have subcommands: %v", validSubcommands) | ||
} | ||
return nil | ||
}, | ||
}, | ||
{ | ||
name: "shows help when no subcommand is provided", | ||
command: NewRootCommand(), | ||
validationFunc: func(cmd *cobra.Command) error { | ||
output := new(bytes.Buffer) | ||
cmd.SetOut(output) | ||
cmd.SetArgs(make([]string, 0)) | ||
err := cmd.Execute() | ||
if err != nil { | ||
return fmt.Errorf("should not return error: %v", err) | ||
} | ||
if !strings.Contains(output.String(), "help for instagram") { | ||
return fmt.Errorf("should show help, but got:\n%s", output) | ||
} | ||
return nil | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := tt.validationFunc(tt.command); err != nil { | ||
t.Errorf("NewRootCommand() validation failed: %v", err) | ||
} | ||
}) | ||
} | ||
} |