Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
Added list, prune, disable, and remove tests.
  • Loading branch information
trgeiger committed Apr 20, 2024
1 parent 82034aa commit 115de1a
Show file tree
Hide file tree
Showing 15 changed files with 286 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"version": "0.2.0",
"configurations": [
{
"name": "Test enable",
"name": "Test",
"type": "go",
"request": "launch",
"mode": "test",
Expand Down
11 changes: 5 additions & 6 deletions cmd/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cmd
import (
"fmt"
"io"
"os"

"github.com/spf13/afero"
"github.com/spf13/cobra"
Expand All @@ -23,11 +22,11 @@ func NewDisableCmd(fs afero.Fs, out io.Writer) *cobra.Command {
repo, err := app.NewCoprRepo(arg)
if err != nil {
fmt.Fprintln(out, err)
}
err = app.ToggleRepo(repo, fs, out, app.Disabled)
if err != nil {
app.SudoMessage(err, out)
os.Exit(1)
} else {
err = app.ToggleRepo(repo, fs, out, app.Disabled)
if err != nil {
app.SudoMessage(err, out)
}
}
}
},
Expand Down
68 changes: 68 additions & 0 deletions cmd/disable_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cmd

import (
"bytes"
"testing"

"github.com/trgeiger/copr-tool/internal/testutil"
)

func TestDisableCmd(t *testing.T) {
tests := []struct {
name string
args []string
repoFiles [][]string // format: file/reponame, test directory folder
otherFiles [][]string // format: filename, path, test directory folder
expected string
}{
{
name: "Disable invalid repo name",
args: []string{
"copr-tool",
},
expected: "invalid repository name: copr-tool\n",
},
{
name: "Repo does not exist",
args: []string{
"example/example",
},
expected: "repository example/example is not installed\n",
},
{
name: "Repo already exists and already disabled",
args: []string{
"kylegospo/bazzite",
},
repoFiles: [][]string{
{"_copr:copr.fedorainfracloud.org:kylegospo:bazzite.repo", "disabled"},
},
expected: "Repository kylegospo/bazzite is already disabled.\n",
},
{
name: "Repo already exists but not disabled",
args: []string{
"kylegospo/bazzite",
},
repoFiles: [][]string{
{"_copr:copr.fedorainfracloud.org:kylegospo:bazzite.repo", "enabled"},
},
expected: "Repository kylegospo/bazzite disabled.\n",
},
}

for _, test := range tests {

b := new(bytes.Buffer)
fs := testutil.AssembleTestFs(test.repoFiles, test.otherFiles)
cmd := NewDisableCmd(fs, b)
cmd.SetOut(b)
cmd.SetArgs(test.args)

cmd.Execute()

if b.String() != test.expected {
t.Fatalf("Test \"%s\" failed", test.name)
}
}
}
3 changes: 0 additions & 3 deletions cmd/enable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"bytes"
"fmt"
"testing"

"github.com/trgeiger/copr-tool/internal/testutil"
Expand Down Expand Up @@ -82,8 +81,6 @@ func TestEnableCmd(t *testing.T) {

cmd.Execute()

outB := b.String()
fmt.Print(outB)
if b.String() != test.expected {
t.Fatalf("Test: \"%s\" failed", test.name)
}
Expand Down
22 changes: 13 additions & 9 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@ func NewListCmd(fs afero.Fs, out io.Writer) *cobra.Command {
if err != nil {
fmt.Fprintf(out, "Error when retrieving locally installed repositories: %s", err)
}
showDupesMessage := false
for _, r := range repos {
r.FindLocalFiles(fs)
if len(r.LocalFiles) > 1 {
showDupesMessage = true
if len(repos) == 0 {
fmt.Fprintln(out, "No installed Copr repositories.")
} else {
showDupesMessage := false
for _, r := range repos {
r.FindLocalFiles(fs)
if len(r.LocalFiles) > 1 {
showDupesMessage = true
}
fmt.Fprintln(out, r.Name())
}
if showDupesMessage {
fmt.Fprintln(out, "\nDuplicate entries found. Consider running the prune command.")
}
fmt.Fprintln(out, r.Name())
}
if showDupesMessage {
fmt.Fprintln(out, "\nDuplicate entries found. Consider running the prune command.")
}
},
}
Expand Down
45 changes: 45 additions & 0 deletions cmd/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cmd

import (
"bytes"
"testing"

"github.com/trgeiger/copr-tool/internal/testutil"
)

func TestListCmd(t *testing.T) {
tests := []struct {
name string
repoFiles [][]string // format: file/reponame, test directory folder
otherFiles [][]string // format: filename, path, test directory folder
expected string
}{
{
name: "List existing repos",
repoFiles: [][]string{
{"_copr:copr.fedorainfracloud.org:kylegospo:bazzite.repo", "enabled"},
{"_copr:copr.fedorainfracloud.org:bieszczaders:kernel-cachyos.repo", "enabled"},
},
expected: "bieszczaders/kernel-cachyos\nkylegospo/bazzite\n",
},
{
name: "No repos to list",
expected: "No installed Copr repositories.\n",
},
}

for _, test := range tests {

b := new(bytes.Buffer)
fs := testutil.AssembleTestFs(test.repoFiles, test.otherFiles)
cmd := NewListCmd(fs, b)
cmd.SetOut(b)

cmd.Execute()

if b.String() != test.expected {
t.Fatalf("Test \"%s\" failed", test.name)
}
}

}
2 changes: 0 additions & 2 deletions cmd/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ func NewPruneCmd(fs afero.Fs, out io.Writer) *cobra.Command {
pruneCount++
} else if pruned && err != nil {
fmt.Fprintf(out, "Pruning attempted on %s but encountered error: %s", r.Name(), err)
os.Exit(1)
} else if err != nil {
fmt.Fprintf(out, "Error encountered: %s", err)
os.Exit(1)
}
}
if pruneCount == 0 {
Expand Down
53 changes: 53 additions & 0 deletions cmd/prune_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cmd

import (
"bytes"
"testing"

"github.com/trgeiger/copr-tool/internal/testutil"
)

func TestPruneCmd(t *testing.T) {
tests := []struct {
name string
repoFiles [][]string // format: file/reponame, test directory folder
otherFiles [][]string // format: filename, path, test directory folder
expected string
}{
{
name: "No repositories installed",
expected: "Nothing to prune.\n",
},
{
name: "Remove 1 duplicate",
repoFiles: [][]string{
{"_copr:copr.fedorainfracloud.org:kylegospo:bazzite.repo", "enabled"},
{"_copr:copr.fedorainfracloud.org:kylegospo:bazzite-copy.repo", "enabled"},
},
expected: "Removed 1 duplicate entry for kylegospo/bazzite.\n",
},
{
name: "Remove multiple duplicates",
repoFiles: [][]string{
{"_copr:copr.fedorainfracloud.org:kylegospo:bazzite.repo", "enabled"},
{"_copr:copr.fedorainfracloud.org:kylegospo:bazzite-copy.repo", "enabled"},
{"_copr:copr.fedorainfracloud.org:kylegospo:bazzite-copy2.repo", "enabled"},
},
expected: "Removed 2 duplicate entries for kylegospo/bazzite.\n",
},
}

for _, test := range tests {

b := new(bytes.Buffer)
fs := testutil.AssembleTestFs(test.repoFiles, test.otherFiles)
cmd := NewPruneCmd(fs, b)
cmd.SetOut(b)

cmd.Execute()

if b.String() != test.expected {
t.Fatalf("Test \"%s\" failed", test.name)
}
}
}
11 changes: 5 additions & 6 deletions cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cmd
import (
"fmt"
"io"
"os"

"github.com/spf13/afero"
"github.com/spf13/cobra"
Expand All @@ -24,11 +23,11 @@ func NewRemoveCmd(fs afero.Fs, out io.Writer) *cobra.Command {
repo, err := app.NewCoprRepo(arg)
if err != nil {
fmt.Fprintln(out, err)
}
err = app.DeleteRepo(repo, fs, out)
if err != nil {
app.SudoMessage(err, out)
os.Exit(1)
} else {
err = app.DeleteRepo(repo, fs, out)
if err != nil {
app.SudoMessage(err, out)
}
}
}
},
Expand Down
58 changes: 58 additions & 0 deletions cmd/remove_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cmd

import (
"bytes"
"testing"

"github.com/trgeiger/copr-tool/internal/testutil"
)

func TestRemoveCmd(t *testing.T) {
tests := []struct {
name string
args []string
repoFiles [][]string // format: file/reponame, test directory folder
otherFiles [][]string // format: filename, path, test directory folder
expected string
}{
{
name: "Remove invalid repo name",
args: []string{
"copr-tool",
},
expected: "invalid repository name: copr-tool\n",
},
{
name: "Remove uninstalled repo",
args: []string{
"example/example",
},
expected: "Repository example/example does not exist locally. Nothing to delete.\n",
},
{
name: "Remove installed repo",
args: []string{
"kylegospo/bazzite",
},
repoFiles: [][]string{
{"_copr:copr.fedorainfracloud.org:kylegospo:bazzite.repo", "enabled"},
},
expected: "Repository kylegospo/bazzite deleted.\n",
},
}

for _, test := range tests {

b := new(bytes.Buffer)
fs := testutil.AssembleTestFs(test.repoFiles, test.otherFiles)
cmd := NewRemoveCmd(fs, b)
cmd.SetOut(b)
cmd.SetArgs(test.args)

cmd.Execute()

if b.String() != test.expected {
t.Fatalf("Test \"%s\" failed", test.name)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[copr:copr.fedorainfracloud.org:bieszczaders:kernel-cachyos]
name=Copr repo for kernel-cachyos owned by bieszczaders
baseurl=https://download.copr.fedorainfracloud.org/results/bieszczaders/kernel-cachyos/fedora-$releasever-$basearch/
type=rpm-md
skip_if_unavailable=True
gpgcheck=1
gpgkey=https://download.copr.fedorainfracloud.org/results/bieszczaders/kernel-cachyos/pubkey.gpg
repo_gpgcheck=0
enabled=1
enabled_metadata=1
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[copr:copr.fedorainfracloud.org:kylegospo:bazzite]
name=Copr repo for bazzite owned by kylegospo
baseurl=https://download.copr.fedorainfracloud.org/results/kylegospo/bazzite/fedora-$releasever-$basearch/
type=rpm-md
skip_if_unavailable=True
gpgcheck=1
gpgkey=https://download.copr.fedorainfracloud.org/results/kylegospo/bazzite/pubkey.gpg
repo_gpgcheck=0
enabled=1
enabled_metadata=1
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[copr:copr.fedorainfracloud.org:kylegospo:bazzite]
name=Copr repo for bazzite owned by kylegospo
baseurl=https://download.copr.fedorainfracloud.org/results/kylegospo/bazzite/fedora-$releasever-$basearch/
type=rpm-md
skip_if_unavailable=True
gpgcheck=1
gpgkey=https://download.copr.fedorainfracloud.org/results/kylegospo/bazzite/pubkey.gpg
repo_gpgcheck=0
enabled=1
enabled_metadata=1
6 changes: 5 additions & 1 deletion internal/app/copr-repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ func (c *CoprRepo) PruneDuplicates(fs afero.Fs, out io.Writer) (bool, error) {
//TODO remove the element from LocalFiles
}
}
fmt.Fprintf(out, "Pruned %d duplicate entries for %s\n", pruneCount, c.Name())
if pruneCount == 1 {
fmt.Fprintf(out, "Removed 1 duplicate entry for %s.\n", c.Name())
} else if pruneCount > 1 {
fmt.Fprintf(out, "Removed %d duplicate entries for %s.\n", pruneCount, c.Name())
}
return true, nil
}
return false, nil
Expand Down
Loading

0 comments on commit 115de1a

Please sign in to comment.