Skip to content

Commit

Permalink
Add more unit tests - cache, config, ghrest, cli, action, image
Browse files Browse the repository at this point in the history
Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com>
  • Loading branch information
rdimitrov committed May 29, 2024
1 parent b5f98fb commit f5908a0
Show file tree
Hide file tree
Showing 11 changed files with 1,316 additions and 132 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
github.com/docker/docker v24.0.9+incompatible // indirect
github.com/docker/docker-credential-helpers v0.7.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/compress v1.16.5 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
Expand All @@ -36,4 +37,5 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/vbatts/tar-split v0.11.3 // indirect
golang.org/x/sys v0.15.0 // indirect
gopkg.in/h2non/gock.v1 v1.1.2 // indirect
)
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ github.com/google/go-github/v61 v61.0.0 h1:VwQCBwhyE9JclCI+22/7mLB1PuU9eowCXKY5p
github.com/google/go-github/v61 v61.0.0/go.mod h1:0WR+KmsWX75G2EbpyGsGmradjo3IiciuI4BmdVCobQY=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/klauspost/compress v1.16.5 h1:IFV2oUNUzZaz+XyusxpLzpzS8Pt5rh0Z16For/djlyI=
Expand All @@ -41,6 +43,7 @@ github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/Qd
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI=
Expand Down Expand Up @@ -91,6 +94,8 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY=
gopkg.in/h2non/gock.v1 v1.1.2/go.mod h1:n7UGz/ckNChHiK05rDoiC4MYSunEC/lyaUm2WWaDva0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
223 changes: 223 additions & 0 deletions internal/cli/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
package cli

import (
"path/filepath"
"strings"
"testing"

"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestNewHelper(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
cmdArgs []string
expected *Helper
expectedError bool
}{
{
name: "ValidFlags",
cmdArgs: []string{"--dry-run", "--quiet", "--error", "--regex", "test"},
expected: &Helper{
DryRun: true,
Quiet: true,
ErrOnModified: true,
Regex: "test",
},
expectedError: false,
},
{
name: "MissingFlags",
cmdArgs: []string{},
expected: &Helper{},
expectedError: false,
},
{
name: "InvalidFlags",
cmdArgs: []string{"--nonexistent"},
expected: nil,
expectedError: true,
},
}

for _, tt := range testCases {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

cmd := &cobra.Command{}
DeclareFrizbeeFlags(cmd, true)
cmd.SetArgs(tt.cmdArgs)

if tt.expectedError {
assert.Error(t, cmd.Execute())
return
}

assert.NoError(t, cmd.Execute())

helper, err := NewHelper(cmd)
if tt.expectedError {
assert.Error(t, err)
assert.Nil(t, helper)
} else {
assert.NoError(t, err)
assert.NotNil(t, helper)
assert.Equal(t, tt.expected.DryRun, helper.DryRun)
assert.Equal(t, tt.expected.Quiet, helper.Quiet)
assert.Equal(t, tt.expected.ErrOnModified, helper.ErrOnModified)
assert.Equal(t, tt.expected.Regex, helper.Regex)
}
})
}
}

func TestProcessOutput(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
helper *Helper
path string
processed []string
modified map[string]string
expectedOutput string
expectError bool
}{
{
name: "QuietMode",
helper: &Helper{
Quiet: true,
Cmd: &cobra.Command{},
},
path: "test/path",
processed: []string{"file1.txt", "file2.txt"},
modified: map[string]string{"file1.txt": "new content"},
expectedOutput: "",
expectError: false,
},
{
name: "DryRunMode",
helper: &Helper{
Quiet: false,
DryRun: true,
Cmd: &cobra.Command{},
},
path: "test/path",
processed: []string{"file1.txt"},
modified: map[string]string{"file1.txt": "new content"},
expectedOutput: "Processed: file1.txt\nModified: file1.txt\nnew content",
expectError: false,
},
{
name: "ErrorOpeningFile",
helper: &Helper{
Quiet: false,
Cmd: &cobra.Command{},
},
path: "invalid/path",
modified: map[string]string{"invalid/path": "new content"},
expectedOutput: "",
expectError: true,
},
}

for _, tt := range testCases {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

// Set up command output
var output strings.Builder
tt.helper.Cmd.SetOut(&output)
tt.helper.Cmd.SetErr(&output)

// Create in-memory filesystem and add files
fs := memfs.New()
for path, content := range tt.modified {
dir := filepath.Join(tt.path, filepath.Dir(path))
assert.NoError(t, fs.MkdirAll(dir, 0755))
file, err := fs.Create(filepath.Join(tt.path, path))
if err == nil {
_, _ = file.Write([]byte(content))
assert.NoError(t, file.Close())
}
}

// Process the output using the in-memory filesystem
err := tt.helper.ProcessOutput(tt.path, tt.processed, tt.modified)
if tt.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Contains(t, output.String(), tt.expectedOutput)
}
})
}
}

func TestIsPath(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
setup func(fs billy.Filesystem)
path string
expected bool
}{
{
name: "ExistingFile",
setup: func(fs billy.Filesystem) {
file, _ := fs.Create("testfile.txt")
assert.NoError(t, file.Close())
},
path: "testfile.txt",
expected: true,
},
{
name: "NonExistentFile",
setup: func(_ billy.Filesystem) {},
path: "nonexistent.txt",
expected: false,
},
{
name: "ExistingDirectory",
setup: func(fs billy.Filesystem) {
assert.NoError(t, fs.MkdirAll("testdir", 0755))
},
path: "testdir",
expected: true,
},
{
name: "NonExistentDirectory",
setup: func(_ billy.Filesystem) {},
path: "nonexistentdir",
expected: false,
},
}

for _, tt := range testCases {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

// Use in-memory filesystem for testing
fs := memfs.New()
tt.setup(fs)

// Check if the path exists in the in-memory filesystem
_, err := fs.Stat(tt.path)
result := err == nil

assert.Equal(t, tt.expected, result)
})
}
}
Loading

0 comments on commit f5908a0

Please sign in to comment.