-
Notifications
You must be signed in to change notification settings - Fork 27
Unit tests for the git store #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,6 @@ lint: | |
--enable=misspell \ | ||
--enable=gas \ | ||
--enable=goimports \ | ||
--enable=gocyclo \ | ||
./... | ||
|
||
fmt: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package git | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/spf13/afero" | ||
|
||
"github.com/bpineau/katafygio/config" | ||
"github.com/bpineau/katafygio/pkg/log" | ||
) | ||
|
||
var testHasGit bool | ||
|
||
func init() { | ||
// Thanks to Mitchell Hashimoto! | ||
if _, err := exec.LookPath("git"); err == nil { | ||
testHasGit = true | ||
} | ||
} | ||
|
||
func TestGitDryRun(t *testing.T) { | ||
appFs = afero.NewMemMapFs() | ||
|
||
conf := &config.KfConfig{ | ||
DryRun: true, | ||
Logger: log.New("info", "", "test"), | ||
LocalDir: "/tmp/ktest", // fake dir (in memory fs provided by Afero) | ||
} | ||
|
||
repo, err := New(conf).Start() | ||
if err != nil { | ||
t.Errorf("failed to start git: %v", err) | ||
} | ||
|
||
_, err = repo.Status() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
repo.Stop() | ||
} | ||
|
||
// testing with real git repositories and commands | ||
func TestGit(t *testing.T) { | ||
if !testHasGit { | ||
t.Log("git not found, skipping") | ||
t.Skip() | ||
} | ||
|
||
dir, err := ioutil.TempDir("", "katafygio-tests") | ||
if err != nil { | ||
t.Fatal("failed to create a temp dir for tests") | ||
} | ||
|
||
defer os.RemoveAll(dir) | ||
|
||
conf := &config.KfConfig{ | ||
Logger: log.New("info", "", "test"), | ||
LocalDir: dir, | ||
} | ||
|
||
repo, err := New(conf).Start() | ||
if err != nil { | ||
t.Errorf("failed to start git: %v", err) | ||
} | ||
|
||
changed, err := repo.Status() | ||
if changed || err != nil { | ||
t.Errorf("Status should return false on empty new repos (%v)", err) | ||
} | ||
|
||
_ = ioutil.WriteFile(dir+"/t.yaml", []byte{42}, 0600) | ||
|
||
changed, err = repo.Status() | ||
if !changed || err != nil { | ||
t.Errorf("Status should return true on non committed files (%v)", err) | ||
} | ||
|
||
changed, err = repo.Commit() | ||
if !changed || err != nil { | ||
t.Errorf("Commit should notify changes and not fail (%v)", err) | ||
} | ||
|
||
changed, err = repo.Status() | ||
if changed || err != nil { | ||
t.Errorf("Status should return false after a add+commit (%v)", err) | ||
} | ||
|
||
changed, err = repo.Commit() | ||
if changed || err != nil { | ||
t.Errorf("Commit shouldn't notify changes on unchanged repos (%v)", err) | ||
} | ||
|
||
// re-use the previous repos for clone tests | ||
|
||
newdir, err := ioutil.TempDir("", "katafygio-tests") | ||
if err != nil { | ||
t.Fatal("failed to create a temp dir for tests") | ||
} | ||
|
||
defer os.RemoveAll(newdir) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Error return value is not checked |
||
|
||
repo.LocalDir = newdir | ||
repo.URL = dir | ||
|
||
err = repo.Clone() | ||
if err != nil { | ||
t.Errorf("clone failed: %v", err) | ||
} | ||
|
||
_ = ioutil.WriteFile(newdir+"/t2.yaml", []byte{42}, 0600) | ||
repo.commitAndPush() | ||
|
||
changed, err = repo.Status() | ||
if changed || err != nil { | ||
t.Errorf("Status should return false after a add+commit+push (%v)", err) | ||
} | ||
|
||
repo.Stop() | ||
|
||
// test various failure modes | ||
|
||
_, err = repo.Start() | ||
if err == nil { | ||
t.Error("Start/Clone on an existing repository should fail") | ||
} | ||
|
||
err = repo.Git("fortzob", "42") | ||
if err == nil { | ||
t.Error("Git should fail with unknown subcommands") | ||
} | ||
|
||
if err == nil { | ||
t.Error("clone should fail on existing repos") | ||
} | ||
|
||
notrepo, err := ioutil.TempDir("", "katafygio-tests") | ||
if err != nil { | ||
t.Fatal("failed to create a temp dir for tests") | ||
} | ||
|
||
defer os.RemoveAll(notrepo) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Error return value is not checked |
||
|
||
repo.LocalDir = notrepo | ||
_, err = repo.Status() | ||
if err == nil { | ||
t.Error("Status should fail on a non-repos") | ||
} | ||
repo.commitAndPush() | ||
_, err = repo.Commit() | ||
if err == nil { | ||
t.Error("Commit should fail on a non-repos") | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error return value is not checked