Add new file
This commit is contained in:
@@ -0,0 +1,206 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/rockliang/poimen/workflows/action"
|
||||
)
|
||||
|
||||
func TestGitCloneAndFetch(t *testing.T) {
|
||||
// Create a temporary directory for the test
|
||||
tmpDir := t.TempDir()
|
||||
sourceDir := filepath.Join(tmpDir, "source")
|
||||
targetDir := filepath.Join(tmpDir, "target")
|
||||
|
||||
// Initialize source repo
|
||||
if err := os.MkdirAll(sourceDir, 0755); err != nil {
|
||||
t.Fatalf("failed to create source dir: %v", err)
|
||||
}
|
||||
|
||||
cmd := exec.Command("git", "init", sourceDir)
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Fatalf("git init failed: %v", err)
|
||||
}
|
||||
|
||||
// Configure git user
|
||||
exec.Command("git", "-C", sourceDir, "config", "user.email", "[email protected]").Run()
|
||||
exec.Command("git", "-C", sourceDir, "config", "user.name", "Test User").Run()
|
||||
|
||||
// Create initial commit
|
||||
testFile := filepath.Join(sourceDir, "test.txt")
|
||||
if err := os.WriteFile(testFile, []byte("test content"), 0644); err != nil {
|
||||
t.Fatalf("failed to create test file: %v", err)
|
||||
}
|
||||
|
||||
cmd = exec.Command("git", "-C", sourceDir, "add", "test.txt")
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Fatalf("git add failed: %v", err)
|
||||
}
|
||||
|
||||
cmd = exec.Command("git", "-C", sourceDir, "commit", "-m", "initial commit")
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Fatalf("git commit failed: %v", err)
|
||||
}
|
||||
|
||||
// Test clone into empty path
|
||||
ctx := context.Background()
|
||||
err := action.CloneRepoActivity(ctx, action.CloneRepoInput{
|
||||
RemoteURL: sourceDir,
|
||||
TargetRepoPath: targetDir,
|
||||
})
|
||||
assert.NoError(t, err, "clone should succeed")
|
||||
|
||||
// Verify .git exists
|
||||
gitDir := filepath.Join(targetDir, ".git")
|
||||
_, err = os.Stat(gitDir)
|
||||
assert.NoError(t, err, ".git directory should exist")
|
||||
|
||||
// Verify test.txt was cloned
|
||||
targetTestFile := filepath.Join(targetDir, "test.txt")
|
||||
_, err = os.Stat(targetTestFile)
|
||||
assert.NoError(t, err, "test.txt should exist in target")
|
||||
|
||||
// Add another commit to source
|
||||
testFile2 := filepath.Join(sourceDir, "test2.txt")
|
||||
if err := os.WriteFile(testFile2, []byte("test content 2"), 0644); err != nil {
|
||||
t.Fatalf("failed to create test2 file: %v", err)
|
||||
}
|
||||
|
||||
cmd = exec.Command("git", "-C", sourceDir, "add", "test2.txt")
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Fatalf("git add failed: %v", err)
|
||||
}
|
||||
|
||||
cmd = exec.Command("git", "-C", sourceDir, "commit", "-m", "second commit")
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Fatalf("git commit failed: %v", err)
|
||||
}
|
||||
|
||||
// Test fetch on existing repo
|
||||
err = action.CloneRepoActivity(ctx, action.CloneRepoInput{
|
||||
RemoteURL: sourceDir,
|
||||
TargetRepoPath: targetDir,
|
||||
})
|
||||
assert.NoError(t, err, "fetch should succeed")
|
||||
}
|
||||
|
||||
func TestGitWorktreeAdd(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
repoDir := filepath.Join(tmpDir, "repo")
|
||||
|
||||
// Initialize repo
|
||||
if err := os.MkdirAll(repoDir, 0755); err != nil {
|
||||
t.Fatalf("failed to create repo dir: %v", err)
|
||||
}
|
||||
|
||||
cmd := exec.Command("git", "init", repoDir)
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Fatalf("git init failed: %v", err)
|
||||
}
|
||||
|
||||
// Configure git user
|
||||
exec.Command("git", "-C", repoDir, "config", "user.email", "[email protected]").Run()
|
||||
exec.Command("git", "-C", repoDir, "config", "user.name", "Test User").Run()
|
||||
|
||||
// Create initial commit
|
||||
testFile := filepath.Join(repoDir, "test.txt")
|
||||
if err := os.WriteFile(testFile, []byte("test"), 0644); err != nil {
|
||||
t.Fatalf("failed to create test file: %v", err)
|
||||
}
|
||||
|
||||
cmd = exec.Command("git", "-C", repoDir, "add", "test.txt")
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Fatalf("git add failed: %v", err)
|
||||
}
|
||||
|
||||
cmd = exec.Command("git", "-C", repoDir, "commit", "-m", "initial")
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Fatalf("git commit failed: %v", err)
|
||||
}
|
||||
|
||||
// Test worktree add
|
||||
ctx := context.Background()
|
||||
worktreePath, err := action.GitWorktreeAddActivity(ctx, action.GitWorktreeAddInput{
|
||||
RepoPath: repoDir,
|
||||
TaskID: "T0.1",
|
||||
})
|
||||
assert.NoError(t, err, "worktree add should succeed")
|
||||
assert.NotEmpty(t, worktreePath, "worktree path should not be empty")
|
||||
|
||||
// Verify worktree directory exists
|
||||
_, err = os.Stat(worktreePath)
|
||||
assert.NoError(t, err, "worktree directory should exist")
|
||||
|
||||
// Verify branch exists
|
||||
cmd = exec.Command("git", "-C", repoDir, "branch", "--list", "task/T0.1")
|
||||
output, err := cmd.CombinedOutput()
|
||||
assert.NoError(t, err, "git branch check should succeed")
|
||||
assert.Contains(t, string(output), "task/T0.1", "task/T0.1 branch should exist")
|
||||
}
|
||||
|
||||
func TestGitCommit(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
repoDir := filepath.Join(tmpDir, "repo")
|
||||
|
||||
// Initialize repo
|
||||
if err := os.MkdirAll(repoDir, 0755); err != nil {
|
||||
t.Fatalf("failed to create repo dir: %v", err)
|
||||
}
|
||||
|
||||
cmd := exec.Command("git", "init", repoDir)
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Fatalf("git init failed: %v", err)
|
||||
}
|
||||
|
||||
// Configure git user
|
||||
exec.Command("git", "-C", repoDir, "config", "user.email", "[email protected]").Run()
|
||||
exec.Command("git", "-C", repoDir, "config", "user.name", "Test User").Run()
|
||||
|
||||
// Create initial commit
|
||||
testFile := filepath.Join(repoDir, "test.txt")
|
||||
if err := os.WriteFile(testFile, []byte("test"), 0644); err != nil {
|
||||
t.Fatalf("failed to create test file: %v", err)
|
||||
}
|
||||
|
||||
cmd = exec.Command("git", "-C", repoDir, "add", "test.txt")
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Fatalf("git add failed: %v", err)
|
||||
}
|
||||
|
||||
cmd = exec.Command("git", "-C", repoDir, "commit", "-m", "initial")
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Fatalf("git commit failed: %v", err)
|
||||
}
|
||||
|
||||
// Create a worktree
|
||||
ctx := context.Background()
|
||||
worktreePath, err := action.GitWorktreeAddActivity(ctx, action.GitWorktreeAddInput{
|
||||
RepoPath: repoDir,
|
||||
TaskID: "T0.1",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Create a new file in the worktree
|
||||
newFile := filepath.Join(worktreePath, "new.txt")
|
||||
if err := os.WriteFile(newFile, []byte("new content"), 0644); err != nil {
|
||||
t.Fatalf("failed to create new file: %v", err)
|
||||
}
|
||||
|
||||
// Commit changes
|
||||
err = action.GitCommitActivity(ctx, action.GitCommitInput{
|
||||
WorktreePath: worktreePath,
|
||||
Message: "Add new file",
|
||||
})
|
||||
assert.NoError(t, err, "commit should succeed")
|
||||
|
||||
// Verify commit exists
|
||||
cmd = exec.Command("git", "-C", worktreePath, "log", "--oneline")
|
||||
output, err := cmd.CombinedOutput()
|
||||
assert.NoError(t, err)
|
||||
assert.Contains(t, string(output), "Add new file", "commit message should be in log")
|
||||
}
|
||||
Reference in New Issue
Block a user