Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
Extend tests to check folder contents. Closes #38.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorinvo committed Sep 29, 2017
1 parent fc6f226 commit c860643
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
37 changes: 37 additions & 0 deletions ghbackup/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ import (
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"testing"

"qvl.io/ghbackup/ghbackup"
)

const (
expectedRepos = " ghbackup homebrew-tap promplot qvl.io slangbrain.com sleepto "
gitFiles = "HEAD config description hooks info objects packed-refs refs"
)

func TestRun(t *testing.T) {
dir, err := ioutil.TempDir("", "qvl-backup")
if err != nil {
Expand All @@ -35,4 +42,34 @@ func TestRun(t *testing.T) {
if err != nil {
t.Error("Unexpected error:", err)
}

// Check contents of backup directory
files, err := ioutil.ReadDir(dir)
if err != nil {
t.Error(err)
}
minLen := len(strings.Split(strings.TrimSpace(expectedRepos), " "))
if len(files) < minLen {
t.Errorf("Expected to fetch at least %d repositories; got %d", minLen, len(files))
}

for _, f := range files {
if !f.IsDir() {
t.Errorf("Expected %s to be a directory", f.Name())
}
strings.Contains(expectedRepos, " "+f.Name()+".git ")
repoFiles, err := ioutil.ReadDir(filepath.Join(dir, f.Name()))
if err != nil {
t.Error(err)
}

var rs []string
for _, r := range repoFiles {
rs = append(rs, r.Name())
}
s := strings.Join(rs, " ")
if s != gitFiles {
t.Errorf("Expected repository for '%s' to contain:\n'%s'\nbut got:\n'%s'", f.Name(), gitFiles, s)
}
}
}
9 changes: 4 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,17 @@ func main() {
os.Exit(1)
}

errs := log.New(os.Stderr, "", 0)
logs := log.New(os.Stdout, "", 0)
logger := log.New(os.Stdout, "", 0)
if *silent {
logs = log.New(ioutil.Discard, "", 0)
logger = log.New(ioutil.Discard, "", 0)
}

err := ghbackup.Run(ghbackup.Config{
Account: *account,
Dir: args[0],
Secret: *secret,
Log: logs,
Err: errs,
Log: logger,
Err: log.New(os.Stderr, "", 0),
})

if err != nil {
Expand Down

0 comments on commit c860643

Please sign in to comment.