File: status_test.go

package info (click to toggle)
golang-github-libgit2-git2go 34.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 920 kB
  • sloc: ansic: 471; sh: 85; makefile: 39
file content (91 lines) | stat: -rw-r--r-- 2,191 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package git

import (
	"io/ioutil"
	"path"
	"testing"
)

func TestStatusFile(t *testing.T) {
	t.Parallel()
	repo := createTestRepo(t)
	defer cleanupTestRepo(t, repo)

	state := repo.State()
	if state != RepositoryStateNone {
		t.Fatal("Incorrect repository state: ", state)
	}

	err := ioutil.WriteFile(path.Join(path.Dir(repo.Workdir()), "hello.txt"), []byte("Hello, World"), 0644)
	checkFatal(t, err)

	status, err := repo.StatusFile("hello.txt")
	checkFatal(t, err)

	if status != StatusWtNew {
		t.Fatal("Incorrect status flags: ", status)
	}
}

func TestStatusList(t *testing.T) {
	t.Parallel()
	repo := createTestRepo(t)
	defer cleanupTestRepo(t, repo)

	// This commits the test repo README, so it doesn't show up in the status list and there's a head to compare to
	seedTestRepo(t, repo)

	err := ioutil.WriteFile(path.Join(path.Dir(repo.Workdir()), "hello.txt"), []byte("Hello, World"), 0644)
	checkFatal(t, err)

	opts := &StatusOptions{}
	opts.Show = StatusShowIndexAndWorkdir
	opts.Flags = StatusOptIncludeUntracked | StatusOptRenamesHeadToIndex | StatusOptSortCaseSensitively

	statusList, err := repo.StatusList(opts)
	checkFatal(t, err)

	entryCount, err := statusList.EntryCount()
	checkFatal(t, err)

	if entryCount != 1 {
		t.Fatal("Incorrect number of status entries: ", entryCount)
	}

	entry, err := statusList.ByIndex(0)
	checkFatal(t, err)
	if entry.Status != StatusWtNew {
		t.Fatal("Incorrect status flags: ", entry.Status)
	}
	if entry.IndexToWorkdir.NewFile.Path != "hello.txt" {
		t.Fatal("Incorrect entry path: ", entry.IndexToWorkdir.NewFile.Path)
	}
}

func TestStatusNothing(t *testing.T) {
	t.Parallel()
	repo := createTestRepo(t)
	defer cleanupTestRepo(t, repo)

	seedTestRepo(t, repo)

	opts := &StatusOptions{
		Show:  StatusShowIndexAndWorkdir,
		Flags: StatusOptIncludeUntracked | StatusOptRenamesHeadToIndex | StatusOptSortCaseSensitively,
	}

	statusList, err := repo.StatusList(opts)
	checkFatal(t, err)

	entryCount, err := statusList.EntryCount()
	checkFatal(t, err)

	if entryCount != 0 {
		t.Fatal("expected no statuses in empty repo")
	}

	_, err = statusList.ByIndex(0)
	if err == nil {
		t.Error("expected error getting status by index")
	}
}