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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/charmbracelet/x/cellbuf v0.0.13 // indirect
github.com/charmbracelet/x/term v0.2.1 // indirect
github.com/cli/safeexec v1.0.1 // indirect
github.com/creack/pty v1.1.24 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/henvic/httpretty v0.1.4 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ github.com/cli/shurcooL-graphql v0.0.4 h1:6MogPnQJLjKkaXPyGqPRXOI2qCsQdqNfUY1QSJ
github.com/cli/shurcooL-graphql v0.0.4/go.mod h1:3waN4u02FiZivIV+p1y4d0Jo1jc6BViMA73C+sZo2fk=
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
Expand Down
248 changes: 248 additions & 0 deletions pkg/cli/compile_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
package cli

import (
"bytes"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"

"github.com/creack/pty"
)

// integrationTestSetup holds the setup state for integration tests
type integrationTestSetup struct {
tempDir string
originalWd string
binaryPath string
workflowsDir string
cleanup func()
}

// setupIntegrationTest creates a temporary directory and builds the gh-aw binary
// This is the equivalent of @Before in Java - common setup for all integration tests
func setupIntegrationTest(t *testing.T) *integrationTestSetup {
t.Helper()

// Create a temporary directory for the test
tempDir, err := os.MkdirTemp("", "gh-aw-compile-integration-*")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}

// Save current working directory and change to temp directory
originalWd, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get current working directory: %v", err)
}
if err := os.Chdir(tempDir); err != nil {
t.Fatalf("Failed to change to temp directory: %v", err)
}

// Build the gh-aw binary
binaryPath := filepath.Join(tempDir, "gh-aw")
buildCmd := exec.Command("make")
projectRoot := filepath.Join(originalWd, "..", "..")
buildCmd.Dir = projectRoot
buildCmd.Stderr = os.Stderr
if err := buildCmd.Run(); err != nil {
t.Fatalf("Failed to build gh-aw binary: %v", err)
}
// move binary to temp directory
if err := os.Rename(filepath.Join(projectRoot, "gh-aw"), binaryPath); err != nil {
t.Fatalf("Failed to move gh-aw binary to temp directory: %v", err)
}

// Create .github/workflows directory
workflowsDir := ".github/workflows"
if err := os.MkdirAll(workflowsDir, 0755); err != nil {
t.Fatalf("Failed to create workflows directory: %v", err)
}

// Setup cleanup function
cleanup := func() {
err := os.Chdir(originalWd)
if err != nil {
t.Fatalf("Failed to change back to original working directory: %v", err)
}
err = os.RemoveAll(tempDir)
if err != nil {
t.Fatalf("Failed to remove temp directory: %v", err)
}
}

return &integrationTestSetup{
tempDir: tempDir,
originalWd: originalWd,
binaryPath: binaryPath,
workflowsDir: workflowsDir,
cleanup: cleanup,
}
}

// TestCompileIntegration tests the compile command by executing the gh-aw CLI binary
func TestCompileIntegration(t *testing.T) {
setup := setupIntegrationTest(t)
defer setup.cleanup()

// Create a test markdown workflow file
testWorkflow := `---
name: Integration Test Workflow
on:
workflow_dispatch:
permissions:
contents: read
engine: claude
---

# Integration Test Workflow

This is a simple integration test workflow.

Please check the repository for any open issues and create a summary.
`

testWorkflowPath := filepath.Join(setup.workflowsDir, "test.md")
if err := os.WriteFile(testWorkflowPath, []byte(testWorkflow), 0644); err != nil {
t.Fatalf("Failed to write test workflow file: %v", err)
}

// Run the compile command
cmd := exec.Command(setup.binaryPath, "compile", testWorkflowPath)
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("CLI compile command failed: %v\nOutput: %s", err, string(output))
}

// Check that the compiled .lock.yml file was created
lockFilePath := filepath.Join(setup.workflowsDir, "test.lock.yml")
if _, err := os.Stat(lockFilePath); os.IsNotExist(err) {
t.Fatalf("Expected lock file %s was not created", lockFilePath)
}

// Read and verify the generated lock file contains expected content
lockContent, err := os.ReadFile(lockFilePath)
if err != nil {
t.Fatalf("Failed to read lock file: %v", err)
}

lockContentStr := string(lockContent)
if !strings.Contains(lockContentStr, "name: \"Integration Test Workflow\"") {
t.Errorf("Lock file should contain the workflow name")
}

if !strings.Contains(lockContentStr, "workflow_dispatch") {
t.Errorf("Lock file should contain the trigger event")
}

if !strings.Contains(lockContentStr, "jobs:") {
t.Errorf("Lock file should contain jobs section")
}

t.Logf("Integration test passed - successfully compiled workflow to %s", lockFilePath)
}

func TestCompileWithIncludeWithEmptyFrontmatterUnderPty(t *testing.T) {
setup := setupIntegrationTest(t)
defer setup.cleanup()

// Create an include file
includeContent := `---
---
# Included Workflow

This is an included workflow file.
`
includeFile := filepath.Join(setup.workflowsDir, "include.md")
if err := os.WriteFile(includeFile, []byte(includeContent), 0644); err != nil {
t.Fatalf("Failed to write include file: %v", err)
}

// Create a test markdown workflow file
testWorkflow := `---
name: Integration Test Workflow
on:
workflow_dispatch:
permissions:
contents: read
engine: claude
---

# Integration Test Workflow

This is a simple integration test workflow.

Please check the repository for any open issues and create a summary.

@include include.md
`
testWorkflowPath := filepath.Join(setup.workflowsDir, "test.md")
if err := os.WriteFile(testWorkflowPath, []byte(testWorkflow), 0644); err != nil {
t.Fatalf("Failed to write test workflow file: %v", err)
}

// Run the compile command
cmd := exec.Command(setup.binaryPath, "compile", testWorkflowPath)
// Start the command with a TTY attached to stdin/stdout/stderr
ptmx, err := pty.Start(cmd)
if err != nil {
t.Fatalf("failed to start PTY: %v", err)
}
defer func() { _ = ptmx.Close() }() // Best effort

// Capture all output from the PTY
var buf bytes.Buffer
done := make(chan struct{})
go func() {
_, _ = io.Copy(&buf, ptmx) // reads both stdout/stderr via the PTY
close(done)
}()

// Wait for the process to finish
err = cmd.Wait()

// Ensure reader goroutine drains remaining output
select {
case <-done:
case <-time.After(750 * time.Millisecond):
}

output := buf.String()
if err != nil {
t.Fatalf("CLI compile command failed: %v\nOutput:\n%s", err, output)
}

// Check that the compiled .lock.yml file was created
lockFilePath := filepath.Join(setup.workflowsDir, "test.lock.yml")
if _, err := os.Stat(lockFilePath); os.IsNotExist(err) {
t.Fatalf("Expected lock file %s was not created", lockFilePath)
}

// Read and verify the generated lock file contains expected content
lockContent, err := os.ReadFile(lockFilePath)
if err != nil {
t.Fatalf("Failed to read lock file: %v", err)
}

lockContentStr := string(lockContent)
if !strings.Contains(lockContentStr, "name: \"Integration Test Workflow\"") {
t.Errorf("Lock file should contain the workflow name")
}

if !strings.Contains(lockContentStr, "workflow_dispatch") {
t.Errorf("Lock file should contain the trigger event")
}

if !strings.Contains(lockContentStr, "jobs:") {
t.Errorf("Lock file should contain jobs section")
}

if strings.Contains(lockContentStr, "\x1b[") {
t.Errorf("Lock file must not contain color escape sequences")
}

t.Logf("Integration test passed - successfully compiled workflow to %s", lockFilePath)
}
113 changes: 111 additions & 2 deletions pkg/parser/frontmatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ func ProcessIncludes(content, baseDir string, extractTools bool) (string, error)
if extractTools {
result.WriteString("{}\n")
} else {
result.WriteString(fmt.Sprintf("\n<!-- Error: %s -->\n\n", err.Error()))
strippedError := StripANSI(err.Error())
result.WriteString(fmt.Sprintf("\n<!-- Error: %s -->\n\n", strippedError))
}
continue
}
Expand All @@ -325,7 +326,8 @@ func ProcessIncludes(content, baseDir string, extractTools bool) (string, error)
if extractTools {
result.WriteString("{}\n")
} else {
result.WriteString(fmt.Sprintf("\n<!-- Error: %s -->\n\n", err.Error()))
strippedError := StripANSI(err.Error())
result.WriteString(fmt.Sprintf("\n<!-- Error: %s -->\n\n", strippedError))
}
continue
}
Expand Down Expand Up @@ -865,3 +867,110 @@ func areEqual(a, b any) bool {

return string(aJSON) == string(bJSON)
}

// StripANSI removes all ANSI escape sequences from a string
// This handles:
// - CSI (Control Sequence Introducer) sequences: \x1b[...
// - OSC (Operating System Command) sequences: \x1b]...\x07 or \x1b]...\x1b\\
// - Simple escape sequences: \x1b followed by a single character
func StripANSI(s string) string {
if s == "" {
return s
}

var result strings.Builder
result.Grow(len(s)) // Pre-allocate capacity for efficiency

i := 0
for i < len(s) {
if s[i] == '\x1b' {
if i+1 >= len(s) {
// ESC at end of string, skip it
i++
continue
}
// Found ESC character, determine sequence type
switch s[i+1] {
case '[':
// CSI sequence: \x1b[...final_char
// Parameters are in range 0x30-0x3F (0-?), intermediate chars 0x20-0x2F (space-/)
// Final characters are in range 0x40-0x7E (@-~)
i += 2 // Skip ESC and [
for i < len(s) {
if isFinalCSIChar(s[i]) {
i++ // Skip the final character
break
} else if isCSIParameterChar(s[i]) {
i++ // Skip parameter/intermediate character
} else {
// Invalid character in CSI sequence, stop processing this escape
break
}
}
case ']':
// OSC sequence: \x1b]...terminator
// Terminators: \x07 (BEL) or \x1b\\ (ST)
i += 2 // Skip ESC and ]
for i < len(s) {
if s[i] == '\x07' {
i++ // Skip BEL
break
} else if s[i] == '\x1b' && i+1 < len(s) && s[i+1] == '\\' {
i += 2 // Skip ESC and \
break
}
i++
}
case '(':
// G0 character set selection: \x1b(char
i += 2 // Skip ESC and (
if i < len(s) {
i++ // Skip the character
}
case ')':
// G1 character set selection: \x1b)char
i += 2 // Skip ESC and )
if i < len(s) {
i++ // Skip the character
}
case '=':
// Application keypad mode: \x1b=
i += 2
case '>':
// Normal keypad mode: \x1b>
i += 2
case 'c':
// Reset: \x1bc
i += 2
default:
// Other escape sequences (2-character)
// Handle common ones like \x1b7, \x1b8, \x1bD, \x1bE, \x1bH, \x1bM
if i+1 < len(s) && (s[i+1] >= '0' && s[i+1] <= '~') {
i += 2
} else {
// Invalid or incomplete escape sequence, just skip ESC
i++
}
}
} else {
// Regular character, keep it
result.WriteByte(s[i])
i++
}
}

return result.String()
}

// isFinalCSIChar checks if a character is a valid CSI final character
// Final characters are in range 0x40-0x7E (@-~)
func isFinalCSIChar(b byte) bool {
return b >= 0x40 && b <= 0x7E
}

// isCSIParameterChar checks if a character is a valid CSI parameter or intermediate character
// Parameter characters are in range 0x30-0x3F (0-?)
// Intermediate characters are in range 0x20-0x2F (space-/)
func isCSIParameterChar(b byte) bool {
return (b >= 0x20 && b <= 0x2F) || (b >= 0x30 && b <= 0x3F)
}
Loading