|
8 | 8 | "context"
|
9 | 9 | "os"
|
10 | 10 | "path/filepath"
|
| 11 | + "strings" |
11 | 12 | "testing"
|
12 | 13 | "time"
|
13 | 14 |
|
@@ -228,6 +229,246 @@ func TestGitStatus(t *testing.T) {
|
228 | 229 | }
|
229 | 230 | }
|
230 | 231 |
|
| 232 | +func TestGitStatusFromFiles(t *testing.T) { |
| 233 | + tests := []struct { |
| 234 | + Name string |
| 235 | + Prep func(context.Context, *Client) error |
| 236 | + Result *Status |
| 237 | + Error error |
| 238 | + }{ |
| 239 | + { |
| 240 | + "no commits", |
| 241 | + func(ctx context.Context, c *Client) error { |
| 242 | + if err := c.Git(ctx, "init"); err != nil { |
| 243 | + return err |
| 244 | + } |
| 245 | + return nil |
| 246 | + }, |
| 247 | + &Status{ |
| 248 | + porcelainStatus: porcelainStatus{ |
| 249 | + BranchOID: "(initial)", |
| 250 | + BranchHead: "master", |
| 251 | + }, |
| 252 | + }, |
| 253 | + nil, |
| 254 | + }, |
| 255 | + { |
| 256 | + "clean copy", |
| 257 | + func(ctx context.Context, c *Client) error { |
| 258 | + if err := initFromRemote(ctx, c); err != nil { |
| 259 | + return err |
| 260 | + } |
| 261 | + return nil |
| 262 | + }, |
| 263 | + &Status{ |
| 264 | + porcelainStatus: porcelainStatus{ |
| 265 | + BranchHead: "master", |
| 266 | + BranchOID: notEmpty, |
| 267 | + }, |
| 268 | + LatestCommit: notEmpty, |
| 269 | + }, |
| 270 | + nil, |
| 271 | + }, |
| 272 | + { |
| 273 | + "untracked files", |
| 274 | + func(ctx context.Context, c *Client) error { |
| 275 | + if err := initFromRemote(ctx, c); err != nil { |
| 276 | + return err |
| 277 | + } |
| 278 | + if err := os.WriteFile(filepath.Join(c.Location, "another-file"), []byte{}, 0755); err != nil { |
| 279 | + return err |
| 280 | + } |
| 281 | + return nil |
| 282 | + }, |
| 283 | + &Status{ |
| 284 | + porcelainStatus: porcelainStatus{ |
| 285 | + BranchHead: "master", |
| 286 | + BranchOID: notEmpty, |
| 287 | + UntrackedFiles: []string{"another-file"}, |
| 288 | + }, |
| 289 | + LatestCommit: notEmpty, |
| 290 | + }, |
| 291 | + nil, |
| 292 | + }, |
| 293 | + { |
| 294 | + "uncommitted files", |
| 295 | + func(ctx context.Context, c *Client) error { |
| 296 | + if err := initFromRemote(ctx, c); err != nil { |
| 297 | + return err |
| 298 | + } |
| 299 | + if err := os.WriteFile(filepath.Join(c.Location, "first-file"), []byte("foobar"), 0755); err != nil { |
| 300 | + return err |
| 301 | + } |
| 302 | + return nil |
| 303 | + }, |
| 304 | + &Status{ |
| 305 | + porcelainStatus: porcelainStatus{ |
| 306 | + BranchHead: "master", |
| 307 | + BranchOID: notEmpty, |
| 308 | + UncommitedFiles: []string{"first-file"}, |
| 309 | + }, |
| 310 | + LatestCommit: notEmpty, |
| 311 | + }, |
| 312 | + nil, |
| 313 | + }, |
| 314 | + { |
| 315 | + "unpushed commits", |
| 316 | + func(ctx context.Context, c *Client) error { |
| 317 | + if err := initFromRemote(ctx, c); err != nil { |
| 318 | + return err |
| 319 | + } |
| 320 | + if err := os.WriteFile(filepath.Join(c.Location, "first-file"), []byte("foobar"), 0755); err != nil { |
| 321 | + return err |
| 322 | + } |
| 323 | + if err := c.Git(ctx, "commit", "-a", "-m", "foo"); err != nil { |
| 324 | + return err |
| 325 | + } |
| 326 | + return nil |
| 327 | + }, |
| 328 | + &Status{ |
| 329 | + porcelainStatus: porcelainStatus{ |
| 330 | + BranchHead: "master", |
| 331 | + BranchOID: notEmpty, |
| 332 | + }, |
| 333 | + UnpushedCommits: []string{notEmpty}, |
| 334 | + LatestCommit: notEmpty, |
| 335 | + }, |
| 336 | + nil, |
| 337 | + }, |
| 338 | + { |
| 339 | + "unpushed commits in new branch", |
| 340 | + func(ctx context.Context, c *Client) error { |
| 341 | + if err := initFromRemote(ctx, c); err != nil { |
| 342 | + return err |
| 343 | + } |
| 344 | + if err := c.Git(ctx, "checkout", "-b", "otherbranch"); err != nil { |
| 345 | + return err |
| 346 | + } |
| 347 | + if err := os.WriteFile(filepath.Join(c.Location, "first-file"), []byte("foobar"), 0755); err != nil { |
| 348 | + return err |
| 349 | + } |
| 350 | + if err := c.Git(ctx, "commit", "-a", "-m", "foo"); err != nil { |
| 351 | + return err |
| 352 | + } |
| 353 | + return nil |
| 354 | + }, |
| 355 | + &Status{ |
| 356 | + porcelainStatus: porcelainStatus{ |
| 357 | + BranchHead: "otherbranch", |
| 358 | + BranchOID: notEmpty, |
| 359 | + }, |
| 360 | + UnpushedCommits: []string{notEmpty}, |
| 361 | + LatestCommit: notEmpty, |
| 362 | + }, |
| 363 | + nil, |
| 364 | + }, |
| 365 | + |
| 366 | + { |
| 367 | + "pending in sub-dir files", |
| 368 | + func(ctx context.Context, c *Client) error { |
| 369 | + if err := initFromRemote(ctx, c); err != nil { |
| 370 | + return err |
| 371 | + } |
| 372 | + if err := os.MkdirAll(filepath.Join(c.Location, "this/is/a/nested/test"), 0755); err != nil { |
| 373 | + return err |
| 374 | + } |
| 375 | + if err := os.WriteFile(filepath.Join(c.Location, "this/is/a/nested/test/first-file"), []byte("foobar"), 0755); err != nil { |
| 376 | + return err |
| 377 | + } |
| 378 | + return nil |
| 379 | + }, |
| 380 | + &Status{ |
| 381 | + porcelainStatus: porcelainStatus{ |
| 382 | + BranchHead: "master", |
| 383 | + BranchOID: notEmpty, |
| 384 | + UntrackedFiles: []string{"this/is/a/nested/test/first-file"}, |
| 385 | + }, |
| 386 | + LatestCommit: notEmpty, |
| 387 | + }, |
| 388 | + nil, |
| 389 | + }, |
| 390 | + } |
| 391 | + |
| 392 | + for _, test := range tests { |
| 393 | + t.Run(test.Name, func(t *testing.T) { |
| 394 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 395 | + defer cancel() |
| 396 | + |
| 397 | + client, err := newGitClient(ctx) |
| 398 | + if err != nil { |
| 399 | + t.Errorf("cannot prep %s: %v", test.Name, err) |
| 400 | + return |
| 401 | + } |
| 402 | + |
| 403 | + err = test.Prep(ctx, client) |
| 404 | + if err != nil { |
| 405 | + t.Errorf("cannot prep %s: %v", test.Name, err) |
| 406 | + return |
| 407 | + } |
| 408 | + |
| 409 | + gitout, err := client.GitWithOutput(ctx, "status", "--porcelain=v2", "--branch", "-uall") |
| 410 | + if err != nil { |
| 411 | + t.Errorf("error calling GitWithOutput: %v", err) |
| 412 | + return |
| 413 | + } |
| 414 | + if err := os.WriteFile(filepath.Join("/tmp", "git_status.txt"), gitout, 0755); err != nil { |
| 415 | + t.Errorf("error creating file: %v", err) |
| 416 | + return |
| 417 | + } |
| 418 | + |
| 419 | + gitout, err = client.GitWithOutput(ctx, "log", "--pretty=%h: %s", "--branches", "--not", "--remotes") |
| 420 | + if err != nil { |
| 421 | + t.Errorf("error calling GitWithOutput: %v", err) |
| 422 | + return |
| 423 | + } |
| 424 | + if err := os.WriteFile(filepath.Join("/tmp", "git_log_1.txt"), gitout, 0755); err != nil { |
| 425 | + t.Errorf("error creating file: %v", err) |
| 426 | + return |
| 427 | + } |
| 428 | + |
| 429 | + gitout, err = client.GitWithOutput(ctx, "log", "--pretty=%H", "-n", "1") |
| 430 | + if err != nil && !strings.Contains(err.Error(), "fatal: your current branch 'master' does not have any commits yet") { |
| 431 | + t.Errorf("error calling GitWithOutput: %v", err) |
| 432 | + return |
| 433 | + } |
| 434 | + if err := os.WriteFile(filepath.Join("/tmp", "git_log_2.txt"), gitout, 0755); err != nil { |
| 435 | + t.Errorf("error creating file: %v", err) |
| 436 | + return |
| 437 | + } |
| 438 | + |
| 439 | + status, err := GitStatusFromFiles(ctx, "/tmp") |
| 440 | + if err != test.Error { |
| 441 | + t.Errorf("expected error does not match for %s: %v != %v", test.Name, err, test.Error) |
| 442 | + return |
| 443 | + } |
| 444 | + |
| 445 | + if status != nil { |
| 446 | + if test.Result.BranchOID == notEmpty && status.LatestCommit != "" { |
| 447 | + test.Result.BranchOID = status.LatestCommit |
| 448 | + } |
| 449 | + if test.Result.LatestCommit == notEmpty && status.LatestCommit != "" { |
| 450 | + test.Result.LatestCommit = status.LatestCommit |
| 451 | + } |
| 452 | + for _, c := range test.Result.UnpushedCommits { |
| 453 | + if c == notEmpty { |
| 454 | + if len(status.UnpushedCommits) == 0 { |
| 455 | + t.Errorf("expected unpushed commits") |
| 456 | + } |
| 457 | + |
| 458 | + test.Result.UnpushedCommits = status.UnpushedCommits |
| 459 | + break |
| 460 | + } |
| 461 | + } |
| 462 | + } |
| 463 | + |
| 464 | + if diff := cmp.Diff(test.Result, status, cmp.AllowUnexported(Status{})); diff != "" { |
| 465 | + t.Errorf("unexpected status (-want +got):\n%s", diff) |
| 466 | + } |
| 467 | + |
| 468 | + }) |
| 469 | + } |
| 470 | +} |
| 471 | + |
231 | 472 | func newGitClient(ctx context.Context) (*Client, error) {
|
232 | 473 | loc, err := os.MkdirTemp("", "gittest")
|
233 | 474 | if err != nil {
|
|
0 commit comments