|
1 | 1 | package config
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
4 | 7 | "testing"
|
5 | 8 |
|
6 | 9 | "github.com/bmatcuk/doublestar/v4"
|
@@ -491,3 +494,151 @@ func TestGetRulesForFileWithArrayConfig(t *testing.T) {
|
491 | 494 | t.Error("expected rule5 to exist")
|
492 | 495 | }
|
493 | 496 | }
|
| 497 | + |
| 498 | +func TestInitDefaultConfig(t *testing.T) { |
| 499 | + t.Run("create config in empty directory", func(t *testing.T) { |
| 500 | + tempDir := t.TempDir() |
| 501 | + |
| 502 | + err := InitDefaultConfig(tempDir) |
| 503 | + if err != nil { |
| 504 | + t.Fatalf("InitDefaultConfig failed: %v", err) |
| 505 | + } |
| 506 | + |
| 507 | + configPath := filepath.Join(tempDir, "rslint.jsonc") |
| 508 | + if _, err := os.Stat(configPath); os.IsNotExist(err) { |
| 509 | + t.Error("rslint.jsonc file was not created") |
| 510 | + } |
| 511 | + |
| 512 | + content, err := os.ReadFile(configPath) |
| 513 | + if err != nil { |
| 514 | + t.Fatalf("failed to read created config file: %v", err) |
| 515 | + } |
| 516 | + |
| 517 | + if string(content) != defaultJsonc { |
| 518 | + t.Error("created config file content does not match expected default content") |
| 519 | + } |
| 520 | + }) |
| 521 | + |
| 522 | + t.Run("fail when config already exists", func(t *testing.T) { |
| 523 | + tempDir := t.TempDir() |
| 524 | + configPath := filepath.Join(tempDir, "rslint.jsonc") |
| 525 | + |
| 526 | + err := os.WriteFile(configPath, []byte("existing content"), 0644) |
| 527 | + if err != nil { |
| 528 | + t.Fatalf("failed to create existing config file: %v", err) |
| 529 | + } |
| 530 | + |
| 531 | + err = InitDefaultConfig(tempDir) |
| 532 | + if err == nil { |
| 533 | + t.Error("expected InitDefaultConfig to fail when config already exists") |
| 534 | + } |
| 535 | + |
| 536 | + expectedErrorMsg := "rslint.json already exists in " + tempDir |
| 537 | + if err.Error() != expectedErrorMsg { |
| 538 | + t.Errorf("expected error message %q, got %q", expectedErrorMsg, err.Error()) |
| 539 | + } |
| 540 | + |
| 541 | + content, err := os.ReadFile(configPath) |
| 542 | + if err != nil { |
| 543 | + t.Fatalf("failed to read config file: %v", err) |
| 544 | + } |
| 545 | + |
| 546 | + if string(content) != "existing content" { |
| 547 | + t.Error("existing config file was modified") |
| 548 | + } |
| 549 | + }) |
| 550 | + |
| 551 | + t.Run("fail with invalid directory", func(t *testing.T) { |
| 552 | + invalidDir := "/nonexistent/directory/path" |
| 553 | + |
| 554 | + err := InitDefaultConfig(invalidDir) |
| 555 | + if err == nil { |
| 556 | + t.Error("expected InitDefaultConfig to fail with invalid directory") |
| 557 | + } |
| 558 | + |
| 559 | + expectedPrefix := "failed to create rslint.json:" |
| 560 | + if !strings.HasPrefix(err.Error(), expectedPrefix) { |
| 561 | + t.Errorf("expected error to start with %q, got %q", expectedPrefix, err.Error()) |
| 562 | + } |
| 563 | + }) |
| 564 | + |
| 565 | + t.Run("create config with relative path", func(t *testing.T) { |
| 566 | + tempDir := t.TempDir() |
| 567 | + |
| 568 | + originalWD, err := os.Getwd() |
| 569 | + if err != nil { |
| 570 | + t.Fatalf("failed to get current working directory: %v", err) |
| 571 | + } |
| 572 | + defer func() { |
| 573 | + t.Chdir(originalWD) |
| 574 | + }() |
| 575 | + |
| 576 | + t.Chdir(tempDir) |
| 577 | + |
| 578 | + err = InitDefaultConfig(".") |
| 579 | + if err != nil { |
| 580 | + t.Fatalf("InitDefaultConfig with relative path failed: %v", err) |
| 581 | + } |
| 582 | + |
| 583 | + configPath := "rslint.jsonc" |
| 584 | + if _, err := os.Stat(configPath); os.IsNotExist(err) { |
| 585 | + t.Error("rslint.jsonc file was not created with relative path") |
| 586 | + } |
| 587 | + }) |
| 588 | + |
| 589 | + t.Run("create config in nested directory", func(t *testing.T) { |
| 590 | + tempDir := t.TempDir() |
| 591 | + |
| 592 | + nestedDir := filepath.Join(tempDir, "project", "config") |
| 593 | + err := os.MkdirAll(nestedDir, 0755) |
| 594 | + if err != nil { |
| 595 | + t.Fatalf("failed to create nested directory: %v", err) |
| 596 | + } |
| 597 | + |
| 598 | + err = InitDefaultConfig(nestedDir) |
| 599 | + if err != nil { |
| 600 | + t.Fatalf("InitDefaultConfig in nested directory failed: %v", err) |
| 601 | + } |
| 602 | + |
| 603 | + configPath := filepath.Join(nestedDir, "rslint.jsonc") |
| 604 | + if _, err := os.Stat(configPath); os.IsNotExist(err) { |
| 605 | + t.Error("rslint.jsonc file was not created in nested directory") |
| 606 | + } |
| 607 | + }) |
| 608 | + |
| 609 | + t.Run("verify config content is valid JSON", func(t *testing.T) { |
| 610 | + tempDir := t.TempDir() |
| 611 | + |
| 612 | + err := InitDefaultConfig(tempDir) |
| 613 | + if err != nil { |
| 614 | + t.Fatalf("InitDefaultConfig failed: %v", err) |
| 615 | + } |
| 616 | + |
| 617 | + configPath := filepath.Join(tempDir, "rslint.jsonc") |
| 618 | + content, err := os.ReadFile(configPath) |
| 619 | + if err != nil { |
| 620 | + t.Fatalf("failed to read config file: %v", err) |
| 621 | + } |
| 622 | + |
| 623 | + if len(content) == 0 { |
| 624 | + t.Error("created config file is empty") |
| 625 | + } |
| 626 | + |
| 627 | + contentStr := string(content) |
| 628 | + expectedElements := []string{ |
| 629 | + "ignores", |
| 630 | + "languageOptions", |
| 631 | + "parserOptions", |
| 632 | + "project", |
| 633 | + "rules", |
| 634 | + "plugins", |
| 635 | + "@typescript-eslint", |
| 636 | + } |
| 637 | + |
| 638 | + for _, element := range expectedElements { |
| 639 | + if !strings.Contains(contentStr, element) { |
| 640 | + t.Errorf("config content missing expected element: %s", element) |
| 641 | + } |
| 642 | + } |
| 643 | + }) |
| 644 | +} |
0 commit comments