package zitcli
import (
"bytes"
"encoding/json"
"strings"
"testing"
)
// A Git import's store appears only when it completes, so its progress
// is zit's own count, read off standard error as the import runs; the
// output still reaches the caller whole.
func TestGitImportProgressFollowsZitsCount(t *testing.T) {
var diag bytes.Buffer
p := &importProgress{dest: "/tmp/x", w: &diag}
if _, _, ok := GitImportProgress("/tmp/x"); ok {
t.Fatal("FAIL: progress before zit counted anything")
}
for _, line := range []string{
"import-git: exporting\n",
"import-git: 192/563 commits (34%), 84/s\n",
"import-git: 320/563 commits (56%), 50/s\nimport-git: 512/563 commits (90%), 80/s\n",
} {
p.Write([]byte(line))
}
if done, total, ok := GitImportProgress("/tmp/x"); !ok || done != 512 || total != 563 {
t.Errorf("FAIL: progress = %d/%d (%v), want the latest count, 512/563", done, total, ok)
}
if !strings.Contains(diag.String(), "192/563") || !strings.HasPrefix(diag.String(), "import-git: exporting") {
t.Error("FAIL: the import's output did not reach the caller whole")
}
gitImports.Delete("/tmp/x")
}
// zit 0.14.13 reports check-ins added and Git commits read apart
// (zit#352); both are read, and an older zit's answer leaves Commits 0.
func TestGitImportReadsCheckinsAndCommitsApart(t *testing.T) {
var r GitImport
if err := json.Unmarshal([]byte(`{"outcome":"changed","added_checkins":1284963,"imported_commits":1499760}`), &r); err != nil {
t.Fatal(err)
}
if r.Added != 1284963 || r.Commits != 1499760 {
t.Errorf("FAIL: read added %d, commits %d", r.Added, r.Commits)
}
var old GitImport
if err := json.Unmarshal([]byte(`{"outcome":"changed","added_checkins":7}`), &old); err != nil {
t.Fatal(err)
}
if old.Added != 7 || old.Commits != 0 {
t.Errorf("FAIL: an older answer read as added %d, commits %d", old.Added, old.Commits)
}
if !t.Failed() {
t.Log("ok")
}
}