Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kleijnweb committed Jun 16, 2018
1 parent b2a1bfb commit e8176c2
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -10,7 +10,7 @@ Dead-simple service for managing Git mirrors, written in Go.

# TODOs

* Tests, tests, tests
* Functional tests
* Lock operations using mutexes
* Better logging
* Recover from panics
Expand Down
32 changes: 23 additions & 9 deletions gmm/git/command_test.go
Expand Up @@ -68,17 +68,31 @@ func TestGitCreateTagArchive(t *testing.T) {
}

func TestGitLsRemoteTags(t *testing.T) {
cmd, _, mockExec := factory()
expected := "lklk"
cmd, _, mockExec := factory()
expected := "lklk"

uri := "https://github.com/sirupsen/logrus"
mockExec.On("Exec", "git", "", "ls-remote", "--tags", uri).Return(expected, nil)
output, err := cmd.LsRemoteTags(uri)
if err != nil {
t.Errorf("unexpected errors: %s", err)
}
uri := "https://github.com/sirupsen/logrus"
mockExec.On("Exec", "git", "", "ls-remote", "--tags", uri).Return(expected, nil)
output, err := cmd.LsRemoteTags(uri)
if err != nil {
t.Errorf("unexpected errors: %s", err)
}

assert.New(t).Equal(expected, output, "they should be equal")
}

func TestGitGetRemote(t *testing.T) {
cmd, _, mockExec := factory()
expected := "lklk"

directory := "/some/path"
mockExec.On("Exec", "git", directory, "config", "--get", "remote.origin.url").Return(expected, nil)
output, err := cmd.GetRemote(directory)
if err != nil {
t.Errorf("unexpected errors: %s", err)
}

assert.New(t).Equal(expected, output, "they should be equal")
assert.New(t).Equal(expected, output, "they should be equal")
}

func TestGitFetchPrune(t *testing.T) {
Expand Down
22 changes: 16 additions & 6 deletions gmm/util/cmd_test.go
Expand Up @@ -2,13 +2,23 @@ package util_test

import (
"github.com/kleijnweb/git-mirror-manager/gmm/util"
"testing"
"github.com/stretchr/testify/assert"
"testing"
)

func TestExec(t *testing.T) {
command := &util.OsCommandExecutor{}
output, err := command.Exec("who", "/", "-u")
if err != nil {
t.Errorf("exec errored: %s", output)
}
command := &util.OsCommandExecutor{}
output, err := command.Exec("who", "/", "-u")
if err != nil {
t.Errorf("exec errored: %s", output)
}
}

func TestExecFailure(t *testing.T) {
command := &util.OsCommandExecutor{}
output, err := command.Exec("this-command-does-not-exist", "/", "-u")

assertions := assert.New(t)
assertions.Error(err)
assertions.Equal("", output)
}
44 changes: 44 additions & 0 deletions gmm/util/filesystem_test.go
@@ -0,0 +1,44 @@
package util

import (
"github.com/stretchr/testify/assert"
"io/ioutil"
"log"
"os"
"testing"
)

func TestOsFileSystemUtil_DirectoryExists(t *testing.T) {
assertions := assert.New(t)
u := OsFileSystemUtil{}
dir, _ := ioutil.TempDir(os.TempDir(), "prefix")
assertions.True(u.DirectoryExists(dir))
os.Remove(dir)
assertions.False(u.DirectoryExists(dir))
}

func TestOsFileSystemUtil_Mkdir(t *testing.T) {
dir, _ := ioutil.TempDir(os.TempDir(), "prefix")
defer os.Remove(dir)
assertions := assert.New(t)
u := OsFileSystemUtil{}
assertions.Nil(u.Mkdir(dir+"/subdir"))
}

func TestOsFileSystemUtil_ReadDir(t *testing.T) {
dir, _ := ioutil.TempDir(os.TempDir(), "prefix")
defer os.Remove(dir)
assertions := assert.New(t)
u := OsFileSystemUtil{}

file, err := os.OpenFile(dir +"/test.txt", os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
log.Fatalf("failed opening file: %s", err)
}
defer file.Close()

slice, err := u.ReadDir(dir)
assertions.Nil(err)

assertions.Equal(slice[0].Name(), "test.txt")
}

0 comments on commit e8176c2

Please sign in to comment.