Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1192,3 +1192,67 @@ Please help.
t.Errorf("Expected 'persona file not found' error message, got: %s", string(output))
}
}

func TestWorkDirOption(t *testing.T) {
// Build the binary
binaryPath := filepath.Join(t.TempDir(), "coding-context")
cmd := exec.Command("go", "build", "-o", binaryPath, ".")
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("failed to build binary: %v\n%s", err, output)
}

// Create a temporary directory structure
tmpDir := t.TempDir()
workDir := filepath.Join(tmpDir, "work")
memoriesDir := filepath.Join(workDir, ".prompts", "memories")
tasksDir := filepath.Join(workDir, ".prompts", "tasks")
outputDir := filepath.Join(tmpDir, "output")

if err := os.MkdirAll(memoriesDir, 0755); err != nil {
t.Fatalf("failed to create memories dir: %v", err)
}
if err := os.MkdirAll(tasksDir, 0755); err != nil {
t.Fatalf("failed to create tasks dir: %v", err)
}

// Create a memory file in the work directory
memoryFile := filepath.Join(memoriesDir, "test.md")
memoryContent := `---
---
# Test Memory
`
if err := os.WriteFile(memoryFile, []byte(memoryContent), 0644); err != nil {
t.Fatalf("failed to write memory file: %v", err)
}

// Create a task file
taskFile := filepath.Join(tasksDir, "task.md")
taskContent := `---
---
# Test Task
`
if err := os.WriteFile(taskFile, []byte(taskContent), 0644); err != nil {
t.Fatalf("failed to write task file: %v", err)
}

// Run the binary with -C option to change to work directory
cmd = exec.Command(binaryPath, "-C", workDir, "-m", ".prompts/memories", "-t", ".prompts/tasks", "-o", outputDir, "task")
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("failed to run binary with -C option: %v\n%s", err, output)
}

// Verify that prompt.md was created in the output directory
promptFile := filepath.Join(outputDir, "prompt.md")
if _, err := os.Stat(promptFile); os.IsNotExist(err) {
t.Errorf("prompt.md was not created in output directory")
}

// Verify the content includes the memory
content, err := os.ReadFile(promptFile)
if err != nil {
t.Fatalf("failed to read prompt.md: %v", err)
}
if !strings.Contains(string(content), "Test Memory") {
t.Errorf("prompt.md does not contain expected memory content")
}
}
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
var bootstrap string

var (
workDir string
memories stringSlice
personas stringSlice
tasks stringSlice
Expand Down Expand Up @@ -63,6 +64,7 @@ func main() {
"/var/local/prompts/tasks",
}

flag.StringVar(&workDir, "C", ".", "Change to directory before doing anything.")
flag.Var(&memories, "m", "Directory containing memories, or a single memory file. Can be specified multiple times.")
flag.Var(&personas, "r", "Directory containing personas, or a single persona file. Can be specified multiple times.")
flag.Var(&tasks, "t", "Directory containing tasks, or a single task file. Can be specified multiple times.")
Expand Down Expand Up @@ -95,6 +97,10 @@ func run(ctx context.Context, args []string) error {
return fmt.Errorf("invalid usage")
}

if err := os.Chdir(workDir); err != nil {
return fmt.Errorf("failed to chdir to %s: %w", workDir, err)
}

// Add task name to includes so memories can be filtered by task
taskName := args[0]
includes["task_name"] = taskName
Expand Down