Skip to content

Commit

Permalink
Merge pull request #8 from hypnoglow/tests
Browse files Browse the repository at this point in the history
Tests + refactoring
  • Loading branch information
hypnoglow committed Mar 2, 2017
2 parents 9e58ca0 + 6f787c2 commit ee39b0a
Show file tree
Hide file tree
Showing 20 changed files with 936 additions and 461 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/dotbro
/go-carpet-coverage*
/cover.out
4 changes: 2 additions & 2 deletions cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ func cleanFiles(dirPath string, files []os.FileInfo) error {

if !removedAny {
removedAny = true
outInfo("Cleaning dead symlinks...")
outputer.OutInfo("Cleaning dead symlinks...")
}

outInfo(" ✓ %s has been removed (broken symlink)", filepath)
outputer.OutInfo(" ✓ %s has been removed (broken symlink)", filepath)
}

return nil
Expand Down
11 changes: 4 additions & 7 deletions cleaner_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package main

import (
"fmt"
"testing"
)
import "testing"

func TestCleanDeadSymlinks(t *testing.T) {
var dirpath string
Expand All @@ -14,22 +11,22 @@ func TestCleanDeadSymlinks(t *testing.T) {
if err == nil {
t.Errorf("[FAIL] cleanDeadSymlinks(%q) -> should error\n", dirpath)
} else {
fmt.Printf("[OK] cleanDeadSymlinks(%q) -> errored with %q\n", dirpath, err)
t.Logf("[OK] cleanDeadSymlinks(%q) -> errored with %q\n", dirpath, err)
}

dirpath = "/home/hypnoglow/.vimrc"
err = cleanDeadSymlinks(dirpath)
if err == nil {
t.Errorf("[FAIL] cleanDeadSymlinks(%q) -> should error\n", dirpath)
} else {
fmt.Printf("[OK] cleanDeadSymlinks(%q) -> errored with %q\n", dirpath, err)
t.Logf("[OK] cleanDeadSymlinks(%q) -> errored with %q\n", dirpath, err)
}

dirpath = "/tmp"
err = cleanDeadSymlinks(dirpath)
if err != nil {
t.Errorf("[FAIL] cleanDeadSymlinks(%q) -> should not error, but errored: %q\n", dirpath, err)
} else {
fmt.Printf("[OK] cleanDeadSymlinks(%q) -> should not error\n", dirpath)
t.Logf("[OK] cleanDeadSymlinks(%q) -> should not error\n", dirpath)
}
}
2 changes: 0 additions & 2 deletions configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ type Files struct {

// NewConfiguration returns a new Configuration.
func NewConfiguration(filename string) (conf *Configuration, err error) {
logger.msg("Parsing config file %s", filename)

switch filepath.Ext(filename) {
case ".toml":
conf, err = fromTOML(filename)
Expand Down
5 changes: 3 additions & 2 deletions docopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import "github.com/docopt/docopt-go"

const version = "0.2.0"

func parseArguments() (map[string]interface{}, error) {
// ParseArguments parses arguments, that were passed to the dotbro, by docopt.
func ParseArguments(argv []string) (map[string]interface{}, error) {
usage := `dotbro - simple yet effective dotfiles manager.
Usage:
Expand All @@ -27,5 +28,5 @@ Other options:
-V --version Show version.
`

return docopt.Parse(usage, nil, true, "dotbro "+version, false)
return docopt.Parse(usage, argv, true, "dotbro "+version, false)
}
10 changes: 10 additions & 0 deletions docopt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import "testing"

func TestParseArguments(t *testing.T) {
_, err := ParseArguments([]string{"--quiet"})
if err != nil {
t.Fatalf("Error parsing arguments: %v", err.Error())
}
}
33 changes: 23 additions & 10 deletions fileutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@ package main
import (
"fmt"
"io"
"os"
"path"
)

// IsExists reports whether path exists (path may be a file or a directory).
func IsExists(osfs OS, path string) (bool, error) {
_, err := osfs.Stat(path)
if osfs.IsNotExist(err) {
return false, nil
}

if err != nil {
return false, err
}

return true, nil
}

// Copy copies a file from src to dst.
func Copy(src, dst string) error {
sfi, err := os.Lstat(src)
func Copy(osfs OS, src, dst string) error {
sfi, err := osfs.Lstat(src)
if err != nil {
return err
}
Expand All @@ -18,9 +31,9 @@ func Copy(src, dst string) error {
return fmt.Errorf("Non-regular source file %s (%q)", sfi.Name(), sfi.Mode().String())
}

dfi, err := os.Stat(dst)
dfi, err := osfs.Stat(dst)
if err != nil {
if !os.IsNotExist(err) {
if !osfs.IsNotExist(err) {
return err
}
// file not exists - do not do anything
Expand All @@ -31,16 +44,16 @@ func Copy(src, dst string) error {
}
}

err = copyFileContents(src, dst)
err = copyFileContents(osfs, src, dst)
return err
}

// copyFileContents copies the contents of the file named src to the file named
// by dst. The file will be created if it does not already exist. If the
// destination file exists, all it's contents will be replaced by the contents
// of the source file.
func copyFileContents(src, dst string) (err error) {
in, err := os.Open(src)
func copyFileContents(osfs OS, src, dst string) (err error) {
in, err := osfs.Open(src)
if err != nil {
return
}
Expand All @@ -52,12 +65,12 @@ func copyFileContents(src, dst string) (err error) {
}
}()

err = os.MkdirAll(path.Dir(dst), 0700)
err = osfs.MkdirAll(path.Dir(dst), 0700)
if err != nil {
return err
}

out, err := os.Create(dst)
out, err := osfs.Create(dst)
if err != nil {
return
}
Expand Down
Loading

0 comments on commit ee39b0a

Please sign in to comment.