Skip to content

Commit

Permalink
test: add tests and display coverage info
Browse files Browse the repository at this point in the history
  • Loading branch information
cecobask committed Oct 6, 2023
1 parent 43a97cc commit 91b0a59
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 15 deletions.
13 changes: 13 additions & 0 deletions .codecov.yaml
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"
4 changes: 1 addition & 3 deletions .github/workflows/insights.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
on:
workflow_dispatch:
env:
ARCHIVE_URL: ${{ secrets.ARCHIVE_URL }}
jobs:
unfollowers:
runs-on: ubuntu-latest
Expand All @@ -17,7 +15,7 @@ jobs:
- name: Add build directory to path
run: echo "${GITHUB_WORKSPACE}/build" >> $GITHUB_PATH
- name: Download Instagram information locally
run: instagram information download $ARCHIVE_URL
run: instagram information download ${{ secrets.ARCHIVE_URL }}
- name: Find out which Instagram users are not following back
run: instagram followdata unfollowers
- name: Cleanup local Instagram information
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/quality.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@ jobs:
go-version: 1.21
- name: Run tests
run: make test
- name: Upload coverage
uses: codecov/codecov-action@v3
with:
files: coverage.out
token: ${{ secrets.CODECOV_TOKEN }}
5 changes: 3 additions & 2 deletions .gitignore
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/
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ lint-fix:
golangci-lint run --fix

test:
go test -race ./...
go test -race -coverprofile=coverage.out -shuffle on ./...
4 changes: 3 additions & 1 deletion cmd/followdata/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"github.com/spf13/cobra"
)

const CommandNameRoot = "followdata"

func NewRootCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "followdata",
Use: CommandNameRoot,
Short: "Instagram follow data operations",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
Expand Down
4 changes: 3 additions & 1 deletion cmd/followdata/unfollowers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"github.com/spf13/cobra"
)

const CommandNameUnfollowers = "unfollowers"

func NewUnfollowersCommand() *cobra.Command {
return &cobra.Command{
Use: "unfollowers",
Use: CommandNameUnfollowers,
Short: "Find out which instagram users are not following back",
RunE: func(cmd *cobra.Command, args []string) error {
return run()
Expand Down
4 changes: 3 additions & 1 deletion cmd/information/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"github.com/spf13/cobra"
)

const CommandNameCleanup = "cleanup"

func NewCleanupCommand() *cobra.Command {
return &cobra.Command{
Use: "cleanup",
Use: CommandNameCleanup,
Short: "Cleanup local instagram information",
RunE: func(cmd *cobra.Command, args []string) error {
err := instagram.CleanupInstagramInformation()
Expand Down
4 changes: 3 additions & 1 deletion cmd/information/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"github.com/spf13/cobra"
)

const CommandNameDownload = "download"

func NewDownloadCommand() *cobra.Command {
return &cobra.Command{
Use: "download <url>",
Use: CommandNameDownload + " <url>",
Short: "Download instagram information locally",
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
Expand Down
6 changes: 2 additions & 4 deletions cmd/information/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package information

import "github.com/spf13/cobra"

type Options struct {
ArchiveDownloadURL string
}
const CommandNameRoot = "information"

func NewRootCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "information",
Use: CommandNameRoot,
Short: "Instagram information operations",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
Expand Down
4 changes: 3 additions & 1 deletion cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"github.com/spf13/cobra"
)

const CommandNameRoot = "instagram"

func NewRootCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "instagram",
Use: CommandNameRoot,
Short: "Instagram Insights CLI",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
Expand Down
67 changes: 67 additions & 0 deletions cmd/root/root_test.go
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)
}
})
}
}

0 comments on commit 91b0a59

Please sign in to comment.