Skip to content

Commit

Permalink
CLI Usage Test
Browse files Browse the repository at this point in the history
  • Loading branch information
amitsaha committed Aug 24, 2023
1 parent 466f6e1 commit a8bab75
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ gitbackup

#
dist/

testdata/**expected
48 changes: 43 additions & 5 deletions cli_test.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,65 @@
package main

import (
"bytes"
"log"
"os"
"os/exec"
"path"
"reflect"
"testing"
)

func TestCli(t *testing.T) {
cmd := exec.Command("go", "build", "-o", "gitbackup_test_bin")
func TestCliUsage(t *testing.T) {
cmd := exec.Command("go", "env")
stdoutStderr, err := cmd.CombinedOutput()
t.Log(cmd.Environ())
t.Fatalf(string(stdoutStderr))

cmd = exec.Command("go", "build", "-v", "-o", "gitbackup_test_bin")
stdoutStderr, err = cmd.CombinedOutput()
if err != nil {
t.Fatal(err)
t.Fatalf("Error building test binary: %v - %v", err, string(stdoutStderr))
}
defer func() {
err := os.Remove("gitbackup_test_bin")
if err != nil {
t.Fatal(err)
}
}()

var stdout, stderr bytes.Buffer
goldenFilepath := path.Join("testdata", t.Name()+".golden")
goldenFilepathNew := goldenFilepath + ".expected"

cmd = exec.Command("./gitbackup_test_bin", "-h")
stdoutStderr, err = cmd.CombinedOutput()
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()
if err != nil {
log.Println(string(stderr.Bytes()))
t.Fatal(err)
}
t.Log(string(stdoutStderr))

gotUsage := stderr.Bytes()

expectedUsage, err := os.ReadFile(goldenFilepath)
if err != nil {
t.Errorf("couldn't read %[1]s..writing expected output to %[1]s", goldenFilepath)
if err := writeExpectedGoldenFile(goldenFilepath, gotUsage); err != nil {
t.Fatal("Error writing file", err)
}
t.FailNow()
}
if !reflect.DeepEqual(expectedUsage, gotUsage) {
t.Errorf("expected and got data mismatch. ..writing expected output to %[1]s", goldenFilepathNew)
if err := writeExpectedGoldenFile(goldenFilepathNew, gotUsage); err != nil {
t.Fatal("Error writing file", err)
}
t.FailNow()
}
}

func writeExpectedGoldenFile(filePath string, data []byte) error {
return os.WriteFile(filePath, data, 0644)
}
33 changes: 33 additions & 0 deletions testdata/TestCliUsage.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Usage of ./gitbackup_test_bin:
-backupdir string
Backup directory
-bare
Clone bare repositories
-githost.url string
DNS of the custom Git host
-github.createUserMigration
Download user data
-github.createUserMigrationRetry
Retry creating the GitHub user migration if we get an error (default true)
-github.createUserMigrationRetryMax int
Number of retries to attempt for creating GitHub user migration (default 5)
-github.listUserMigrations
List available user migrations
-github.namespaceWhitelist string
Organizations/Users from where we should clone (separate each value by a comma: 'user1,org2')
-github.repoType string
Repo types to backup (all, owner, member, starred) (default "all")
-github.waitForUserMigration
Wait for migration to complete (default true)
-gitlab.projectMembershipType string
Project type to clone (all, owner, member, starred) (default "all")
-gitlab.projectVisibility string
Visibility level of Projects to clone (internal, public, private) (default "internal")
-ignore-fork
Ignore repositories which are forks
-ignore-private
Ignore private repositories/projects
-service string
Git Hosted Service Name (github/gitlab/bitbucket)
-use-https-clone
Use HTTPS for cloning instead of SSH

0 comments on commit a8bab75

Please sign in to comment.