197 lines
4.6 KiB
Go
197 lines
4.6 KiB
Go
package commands
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"git.dws.rip/DWS/onyx/internal/core"
|
|
)
|
|
|
|
func TestInitCommand(t *testing.T) {
|
|
// Create a temporary directory for testing
|
|
tempDir := t.TempDir()
|
|
|
|
// Create a repository instance
|
|
repo := &core.OnyxRepository{}
|
|
|
|
// Initialize the repository
|
|
err := repo.Init(tempDir)
|
|
if err != nil {
|
|
t.Fatalf("Failed to initialize repository: %v", err)
|
|
}
|
|
|
|
// Verify .git directory exists
|
|
gitPath := filepath.Join(tempDir, ".git")
|
|
if _, err := os.Stat(gitPath); os.IsNotExist(err) {
|
|
t.Errorf(".git directory was not created")
|
|
}
|
|
|
|
// Verify .onx directory exists
|
|
onyxPath := filepath.Join(tempDir, ".onx")
|
|
if _, err := os.Stat(onyxPath); os.IsNotExist(err) {
|
|
t.Errorf(".onx directory was not created")
|
|
}
|
|
|
|
// Verify oplog file exists
|
|
oplogPath := filepath.Join(onyxPath, "oplog")
|
|
if _, err := os.Stat(oplogPath); os.IsNotExist(err) {
|
|
t.Errorf("oplog file was not created")
|
|
}
|
|
|
|
// Verify workstreams.json exists
|
|
workstreamsPath := filepath.Join(onyxPath, "workstreams.json")
|
|
if _, err := os.Stat(workstreamsPath); os.IsNotExist(err) {
|
|
t.Errorf("workstreams.json was not created")
|
|
}
|
|
|
|
// Verify rerere_cache directory exists
|
|
rererePath := filepath.Join(onyxPath, "rerere_cache")
|
|
if _, err := os.Stat(rererePath); os.IsNotExist(err) {
|
|
t.Errorf("rerere_cache directory was not created")
|
|
}
|
|
}
|
|
|
|
func TestInitCommandInExistingRepo(t *testing.T) {
|
|
// Create a temporary directory for testing
|
|
tempDir := t.TempDir()
|
|
|
|
// Initialize once
|
|
repo := &core.OnyxRepository{}
|
|
err := repo.Init(tempDir)
|
|
if err != nil {
|
|
t.Fatalf("Failed to initialize repository: %v", err)
|
|
}
|
|
|
|
// Verify it's an Onyx repo
|
|
if !core.IsOnyxRepo(tempDir) {
|
|
t.Errorf("IsOnyxRepo returned false for initialized repository")
|
|
}
|
|
}
|
|
|
|
func TestIsOnyxRepo(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
setup func(string) error
|
|
expected bool
|
|
}{
|
|
{
|
|
name: "empty directory",
|
|
setup: func(path string) error {
|
|
return nil
|
|
},
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "initialized repository",
|
|
setup: func(path string) error {
|
|
repo := &core.OnyxRepository{}
|
|
return repo.Init(path)
|
|
},
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "directory with only .git",
|
|
setup: func(path string) error {
|
|
return os.MkdirAll(filepath.Join(path, ".git"), 0755)
|
|
},
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "directory with only .onx",
|
|
setup: func(path string) error {
|
|
return os.MkdirAll(filepath.Join(path, ".onx"), 0755)
|
|
},
|
|
expected: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
tempDir := t.TempDir()
|
|
|
|
err := tt.setup(tempDir)
|
|
if err != nil {
|
|
t.Fatalf("Setup failed: %v", err)
|
|
}
|
|
|
|
result := core.IsOnyxRepo(tempDir)
|
|
if result != tt.expected {
|
|
t.Errorf("IsOnyxRepo() = %v, expected %v", result, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAddToGitignore(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
existingContent string
|
|
entryToAdd string
|
|
shouldContain string
|
|
}{
|
|
{
|
|
name: "add to empty gitignore",
|
|
existingContent: "",
|
|
entryToAdd: ".onx/",
|
|
shouldContain: ".onx/",
|
|
},
|
|
{
|
|
name: "add to existing gitignore",
|
|
existingContent: "node_modules/\n*.log\n",
|
|
entryToAdd: ".onx/",
|
|
shouldContain: ".onx/",
|
|
},
|
|
{
|
|
name: "don't duplicate existing entry",
|
|
existingContent: ".onx/\nnode_modules/\n",
|
|
entryToAdd: ".onx/",
|
|
shouldContain: ".onx/",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
tempDir := t.TempDir()
|
|
gitignorePath := filepath.Join(tempDir, ".gitignore")
|
|
|
|
// Create existing content if specified
|
|
if tt.existingContent != "" {
|
|
err := os.WriteFile(gitignorePath, []byte(tt.existingContent), 0644)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create test .gitignore: %v", err)
|
|
}
|
|
}
|
|
|
|
// Add the entry
|
|
err := addToGitignore(gitignorePath, tt.entryToAdd)
|
|
if err != nil {
|
|
t.Fatalf("addToGitignore failed: %v", err)
|
|
}
|
|
|
|
// Read the result
|
|
content, err := os.ReadFile(gitignorePath)
|
|
if err != nil {
|
|
t.Fatalf("Failed to read .gitignore: %v", err)
|
|
}
|
|
|
|
// Verify the entry is present
|
|
if !containsLine(string(content), tt.shouldContain) {
|
|
t.Errorf(".gitignore does not contain expected entry %q\nContent:\n%s", tt.shouldContain, string(content))
|
|
}
|
|
|
|
// Count occurrences (should not be duplicated)
|
|
lines := splitLines(string(content))
|
|
count := 0
|
|
for _, line := range lines {
|
|
if line == tt.shouldContain {
|
|
count++
|
|
}
|
|
}
|
|
if count > 1 {
|
|
t.Errorf("Entry %q appears %d times, expected 1", tt.shouldContain, count)
|
|
}
|
|
})
|
|
}
|
|
}
|