|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/ioutil" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/spf13/afero" |
| 10 | + |
| 11 | + "github.com/bpineau/katafygio/config" |
| 12 | + "github.com/bpineau/katafygio/pkg/log" |
| 13 | +) |
| 14 | + |
| 15 | +var testHasGit bool |
| 16 | + |
| 17 | +func init() { |
| 18 | + // Thanks to Mitchell Hashimoto! |
| 19 | + if _, err := exec.LookPath("git"); err == nil { |
| 20 | + testHasGit = true |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +func TestGitDryRun(t *testing.T) { |
| 25 | + appFs = afero.NewMemMapFs() |
| 26 | + |
| 27 | + conf := &config.KfConfig{ |
| 28 | + DryRun: true, |
| 29 | + Logger: log.New("info", "", "test"), |
| 30 | + LocalDir: "/tmp/ktest", // fake dir (in memory fs provided by Afero) |
| 31 | + } |
| 32 | + |
| 33 | + repo, err := New(conf).Start() |
| 34 | + if err != nil { |
| 35 | + t.Errorf("failed to start git: %v", err) |
| 36 | + } |
| 37 | + |
| 38 | + _, err = repo.Status() |
| 39 | + if err != nil { |
| 40 | + t.Error(err) |
| 41 | + } |
| 42 | + |
| 43 | + repo.Stop() |
| 44 | +} |
| 45 | + |
| 46 | +// testing with real git repositories and commands |
| 47 | +func TestGit(t *testing.T) { |
| 48 | + if !testHasGit { |
| 49 | + t.Log("git not found, skipping") |
| 50 | + t.Skip() |
| 51 | + } |
| 52 | + |
| 53 | + dir, err := ioutil.TempDir("", "katafygio-tests") |
| 54 | + if err != nil { |
| 55 | + t.Fatal("failed to create a temp dir for tests") |
| 56 | + } |
| 57 | + |
| 58 | + defer os.RemoveAll(dir) |
| 59 | + |
| 60 | + conf := &config.KfConfig{ |
| 61 | + Logger: log.New("info", "", "test"), |
| 62 | + LocalDir: dir, |
| 63 | + } |
| 64 | + |
| 65 | + repo, err := New(conf).Start() |
| 66 | + if err != nil { |
| 67 | + t.Errorf("failed to start git: %v", err) |
| 68 | + } |
| 69 | + |
| 70 | + changed, err := repo.Status() |
| 71 | + if changed || err != nil { |
| 72 | + t.Errorf("Status should return false on empty new repos (%v)", err) |
| 73 | + } |
| 74 | + |
| 75 | + _ = ioutil.WriteFile(dir+"/t.yaml", []byte{42}, 0600) |
| 76 | + |
| 77 | + changed, err = repo.Status() |
| 78 | + if !changed || err != nil { |
| 79 | + t.Errorf("Status should return true on non committed files (%v)", err) |
| 80 | + } |
| 81 | + |
| 82 | + changed, err = repo.Commit() |
| 83 | + if !changed || err != nil { |
| 84 | + t.Errorf("Commit should notify changes and not fail (%v)", err) |
| 85 | + } |
| 86 | + |
| 87 | + changed, err = repo.Status() |
| 88 | + if changed || err != nil { |
| 89 | + t.Errorf("Status should return false after a add+commit (%v)", err) |
| 90 | + } |
| 91 | + |
| 92 | + changed, err = repo.Commit() |
| 93 | + if changed || err != nil { |
| 94 | + t.Errorf("Commit shouldn't notify changes on unchanged repos (%v)", err) |
| 95 | + } |
| 96 | + |
| 97 | + // re-use the previous repos for clone tests |
| 98 | + |
| 99 | + newdir, err := ioutil.TempDir("", "katafygio-tests") |
| 100 | + if err != nil { |
| 101 | + t.Fatal("failed to create a temp dir for tests") |
| 102 | + } |
| 103 | + |
| 104 | + defer os.RemoveAll(newdir) |
| 105 | + |
| 106 | + repo.LocalDir = newdir |
| 107 | + repo.URL = dir |
| 108 | + |
| 109 | + err = repo.Clone() |
| 110 | + if err != nil { |
| 111 | + t.Errorf("clone failed: %v", err) |
| 112 | + } |
| 113 | + |
| 114 | + _ = ioutil.WriteFile(newdir+"/t2.yaml", []byte{42}, 0600) |
| 115 | + repo.commitAndPush() |
| 116 | + |
| 117 | + changed, err = repo.Status() |
| 118 | + if changed || err != nil { |
| 119 | + t.Errorf("Status should return false after a add+commit+push (%v)", err) |
| 120 | + } |
| 121 | + |
| 122 | + repo.Stop() |
| 123 | + |
| 124 | + // test various failure modes |
| 125 | + |
| 126 | + _, err = repo.Start() |
| 127 | + if err == nil { |
| 128 | + t.Error("Start/Clone on an existing repository should fail") |
| 129 | + } |
| 130 | + |
| 131 | + err = repo.Git("fortzob", "42") |
| 132 | + if err == nil { |
| 133 | + t.Error("Git should fail with unknown subcommands") |
| 134 | + } |
| 135 | + |
| 136 | + if err == nil { |
| 137 | + t.Error("clone should fail on existing repos") |
| 138 | + } |
| 139 | + |
| 140 | + notrepo, err := ioutil.TempDir("", "katafygio-tests") |
| 141 | + if err != nil { |
| 142 | + t.Fatal("failed to create a temp dir for tests") |
| 143 | + } |
| 144 | + |
| 145 | + defer os.RemoveAll(notrepo) |
| 146 | + |
| 147 | + repo.LocalDir = notrepo |
| 148 | + _, err = repo.Status() |
| 149 | + if err == nil { |
| 150 | + t.Error("Status should fail on a non-repos") |
| 151 | + } |
| 152 | + repo.commitAndPush() |
| 153 | + _, err = repo.Commit() |
| 154 | + if err == nil { |
| 155 | + t.Error("Commit should fail on a non-repos") |
| 156 | + } |
| 157 | +} |
0 commit comments