|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestStashWorktreeError(t *testing.T) { |
| 12 | + _, err := testrepo.StashList() |
| 13 | + if err == nil { |
| 14 | + t.Errorf("StashList() error = %v, wantErr %v", err, true) |
| 15 | + return |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +func TestStash(t *testing.T) { |
| 20 | + tmp := t.TempDir() |
| 21 | + path, err := filepath.Abs(repoPath) |
| 22 | + if err != nil { |
| 23 | + t.Fatal(err) |
| 24 | + } |
| 25 | + |
| 26 | + if err := Clone("file://"+path, tmp); err != nil { |
| 27 | + t.Fatal(err) |
| 28 | + } |
| 29 | + |
| 30 | + repo, err := Open(tmp) |
| 31 | + if err != nil { |
| 32 | + t.Fatal(err) |
| 33 | + } |
| 34 | + |
| 35 | + if err := os.WriteFile(tmp+"/resources/newfile", []byte("hello, world!"), 0o644); err != nil { |
| 36 | + t.Fatal(err) |
| 37 | + } |
| 38 | + |
| 39 | + f, err := os.OpenFile(tmp+"/README.txt", os.O_APPEND|os.O_WRONLY, 0o644) |
| 40 | + if err != nil { |
| 41 | + t.Fatal(err) |
| 42 | + } |
| 43 | + |
| 44 | + if _, err := f.WriteString("\n\ngit-module"); err != nil { |
| 45 | + t.Fatal(err) |
| 46 | + } |
| 47 | + |
| 48 | + f.Close() |
| 49 | + if err := repo.Add(AddOptions{ |
| 50 | + All: true, |
| 51 | + }); err != nil { |
| 52 | + t.Fatal(err) |
| 53 | + } |
| 54 | + |
| 55 | + if err := repo.StashPush(""); err != nil { |
| 56 | + t.Fatal(err) |
| 57 | + } |
| 58 | + |
| 59 | + f, err = os.OpenFile(tmp+"/README.txt", os.O_APPEND|os.O_WRONLY, 0o644) |
| 60 | + if err != nil { |
| 61 | + t.Fatal(err) |
| 62 | + } |
| 63 | + |
| 64 | + if _, err := f.WriteString("\n\nstash 1"); err != nil { |
| 65 | + t.Fatal(err) |
| 66 | + } |
| 67 | + |
| 68 | + f.Close() |
| 69 | + if err := repo.Add(AddOptions{ |
| 70 | + All: true, |
| 71 | + }); err != nil { |
| 72 | + t.Fatal(err) |
| 73 | + } |
| 74 | + |
| 75 | + if err := repo.StashPush("custom message"); err != nil { |
| 76 | + t.Fatal(err) |
| 77 | + } |
| 78 | + |
| 79 | + want := []*Stash{ |
| 80 | + { |
| 81 | + Index: 0, |
| 82 | + Message: "On master: custom message", |
| 83 | + Files: []string{"README.txt"}, |
| 84 | + }, |
| 85 | + { |
| 86 | + Index: 1, |
| 87 | + Message: "WIP on master: cfc3b29 Add files with same SHA", |
| 88 | + Files: []string{"README.txt", "resources/newfile"}, |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + stash, err := repo.StashList(StashListOptions{ |
| 93 | + CommandOptions: CommandOptions{ |
| 94 | + Envs: []string{"GIT_CONFIG_GLOBAL=/dev/null"}, |
| 95 | + }, |
| 96 | + }) |
| 97 | + require.NoError(t, err) |
| 98 | + require.Equalf(t, want, stash, "StashList() got = %v, want %v", stash, want) |
| 99 | + |
| 100 | + wantDiff := &Diff{ |
| 101 | + totalAdditions: 4, |
| 102 | + totalDeletions: 0, |
| 103 | + isIncomplete: false, |
| 104 | + Files: []*DiffFile{ |
| 105 | + { |
| 106 | + Name: "README.txt", |
| 107 | + Type: DiffFileChange, |
| 108 | + Index: "72e29aca01368bc0aca5d599c31fa8705b11787d", |
| 109 | + OldIndex: "adfd6da3c0a3fb038393144becbf37f14f780087", |
| 110 | + Sections: []*DiffSection{ |
| 111 | + { |
| 112 | + Lines: []*DiffLine{ |
| 113 | + { |
| 114 | + Type: DiffLineSection, |
| 115 | + Content: `@@ -13,3 +13,6 @@ As a quick reminder, this came from one of three locations in either SSH, Git, o`, |
| 116 | + }, |
| 117 | + { |
| 118 | + Type: DiffLinePlain, |
| 119 | + Content: " We can, as an example effort, even modify this README and change it as if it were source code for the purposes of the class.", |
| 120 | + LeftLine: 13, |
| 121 | + RightLine: 13, |
| 122 | + }, |
| 123 | + { |
| 124 | + Type: DiffLinePlain, |
| 125 | + Content: " ", |
| 126 | + LeftLine: 14, |
| 127 | + RightLine: 14, |
| 128 | + }, |
| 129 | + { |
| 130 | + Type: DiffLinePlain, |
| 131 | + Content: " This demo also includes an image with changes on a branch for examination of image diff on GitHub.", |
| 132 | + LeftLine: 15, |
| 133 | + RightLine: 15, |
| 134 | + }, |
| 135 | + { |
| 136 | + Type: DiffLineAdd, |
| 137 | + Content: "+", |
| 138 | + LeftLine: 0, |
| 139 | + RightLine: 16, |
| 140 | + }, |
| 141 | + { |
| 142 | + Type: DiffLineAdd, |
| 143 | + Content: "+", |
| 144 | + LeftLine: 0, |
| 145 | + RightLine: 17, |
| 146 | + }, |
| 147 | + { |
| 148 | + Type: DiffLineAdd, |
| 149 | + Content: "+git-module", |
| 150 | + LeftLine: 0, |
| 151 | + RightLine: 18, |
| 152 | + }, |
| 153 | + }, |
| 154 | + numAdditions: 3, |
| 155 | + numDeletions: 0, |
| 156 | + }, |
| 157 | + }, |
| 158 | + numAdditions: 3, |
| 159 | + numDeletions: 0, |
| 160 | + oldName: "README.txt", |
| 161 | + mode: 0o100644, |
| 162 | + oldMode: 0o100644, |
| 163 | + isBinary: false, |
| 164 | + isSubmodule: false, |
| 165 | + isIncomplete: false, |
| 166 | + }, |
| 167 | + { |
| 168 | + Name: "resources/newfile", |
| 169 | + Type: DiffFileAdd, |
| 170 | + Index: "30f51a3fba5274d53522d0f19748456974647b4f", |
| 171 | + OldIndex: "0000000000000000000000000000000000000000", |
| 172 | + Sections: []*DiffSection{ |
| 173 | + { |
| 174 | + Lines: []*DiffLine{ |
| 175 | + { |
| 176 | + Type: DiffLineSection, |
| 177 | + Content: "@@ -0,0 +1 @@", |
| 178 | + }, |
| 179 | + { |
| 180 | + Type: DiffLineAdd, |
| 181 | + Content: "+hello, world!", |
| 182 | + LeftLine: 0, |
| 183 | + RightLine: 1, |
| 184 | + }, |
| 185 | + }, |
| 186 | + numAdditions: 1, |
| 187 | + numDeletions: 0, |
| 188 | + }, |
| 189 | + }, |
| 190 | + numAdditions: 1, |
| 191 | + numDeletions: 0, |
| 192 | + oldName: "resources/newfile", |
| 193 | + mode: 0o100644, |
| 194 | + oldMode: 0o100644, |
| 195 | + isBinary: false, |
| 196 | + isSubmodule: false, |
| 197 | + isIncomplete: false, |
| 198 | + }, |
| 199 | + }, |
| 200 | + } |
| 201 | + |
| 202 | + diff, err := repo.StashDiff(want[1].Index, 0, 0, 0, DiffOptions{ |
| 203 | + CommandOptions: CommandOptions{ |
| 204 | + Envs: []string{"GIT_CONFIG_GLOBAL=/dev/null"}, |
| 205 | + }, |
| 206 | + }) |
| 207 | + require.NoError(t, err) |
| 208 | + require.Equalf(t, wantDiff, diff, "StashDiff() got = %v, want %v", diff, wantDiff) |
| 209 | +} |
0 commit comments