Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
haitham911 committed Jan 20, 2025
1 parent 201b521 commit 88d043e
Showing 1 changed file with 5 additions and 94 deletions.
99 changes: 5 additions & 94 deletions tests/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"errors"
"flag"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath" // For resolving absolute paths
Expand Down Expand Up @@ -86,13 +84,7 @@ func (m *MatchPattern) UnmarshalYAML(value *yaml.Node) error {
}

func loadTestSuite(filePath string) (*TestSuite, error) {
// Open the file
f, err := os.Open(filePath)
if err != nil {
log.Fatalf("Failed to open file: %v", err)
}
defer f.Close()
data, err := io.ReadAll(f)
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -457,76 +449,6 @@ func TestCLICommands(t *testing.T) {
// Run with `t.Run` for non-TTY tests
t.Run(tc.Name, func(t *testing.T) {
runCLICommandTest(t, tc)
defer func() {
// Change back to the original working directory after the test
if err := os.Chdir(startingDir); err != nil {
t.Fatalf("Failed to change back to the starting directory: %v", err)
}
}()

// Change to the specified working directory
if tc.Workdir != "" {
err := os.Chdir(tc.Workdir)
if err != nil {
t.Fatalf("Failed to change directory to %q: %v", tc.Workdir, err)
}
}

// Check if the binary exists
binaryPath, err := exec.LookPath(tc.Command)
if err != nil {
t.Fatalf("Binary not found: %s. Current PATH: %s", tc.Command, pathManager.GetPath())
}

// Prepare the command
cmd := exec.Command(binaryPath, tc.Args...)

// Set environment variables
envVars := os.Environ()
for key, value := range tc.Env {
envVars = append(envVars, fmt.Sprintf("%s=%s", key, value))
}
cmd.Env = envVars

var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr

// Run the command
err = cmd.Run()

// Validate exit code
exitCode := 0
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
exitCode = exitErr.ExitCode()
}
}
if exitCode != tc.Expect.ExitCode {
t.Errorf("Description: %s", tc.Description)
t.Errorf("Reason: Expected exit code %d, got %d", tc.Expect.ExitCode, exitCode)
t.Errorf("error: %v", cmd.Stderr)
}

// Validate stdout
if !verifyOutput(t, "stdout", stdout.String(), tc.Expect.Stdout) {
t.Errorf("Description: %s", tc.Description)
}

// Validate stderr
if !verifyOutput(t, "stderr", stderr.String(), tc.Expect.Stderr) {
t.Errorf("Description: %s", tc.Description)
}

// Validate file existence
if !verifyFileExists(t, tc.Expect.FileExists) {
t.Errorf("Description: %s", tc.Description)
}

// Validate file contents
if !verifyFileContains(t, tc.Expect.FileContains) {
t.Errorf("Description: %s", tc.Description)
}
})
}
}
Expand Down Expand Up @@ -590,17 +512,12 @@ func verifyOutput(t *testing.T, outputType, output string, patterns []MatchPatte
}
return success
}

func verifyFileExists(t *testing.T, files []string) bool {
success := true
for _, file := range files {
fileAbs, err := filepath.Abs(file)
if err != nil {
log.Println(err)
return false
}

if _, err := os.Stat(fileAbs); errors.Is(err, os.ErrNotExist) {
t.Errorf("Reason: Expected file does not exist: %q", fileAbs)
if _, err := os.Stat(file); errors.Is(err, os.ErrNotExist) {
t.Errorf("Reason: Expected file does not exist: %q", file)
success = false
}
}
Expand All @@ -610,13 +527,7 @@ func verifyFileExists(t *testing.T, files []string) bool {
func verifyFileContains(t *testing.T, filePatterns map[string][]MatchPattern) bool {
success := true
for file, patterns := range filePatterns {
// Open the file
f, err := os.Open(file)
if err != nil {
log.Fatalf("Failed to open file: %v", err)
}
defer f.Close()
content, err := io.ReadAll(f)
content, err := os.ReadFile(file)
if err != nil {
t.Errorf("Reason: Failed to read file %q: %v", file, err)
success = false
Expand Down

0 comments on commit 88d043e

Please sign in to comment.