Skip to content

Commit

Permalink
Fix and Test #3
Browse files Browse the repository at this point in the history
  • Loading branch information
dim0x69 committed Oct 23, 2024
1 parent ed71421 commit ac905be
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
21 changes: 21 additions & 0 deletions exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,28 @@ func loadLaunchers() {
logrus.Debug("Added launchers: ", addedLaunchers)
}

/*
Before executing commandBlock, this function validates that all dependencies are present in the commands map.
*/
func validateDependencies(commands map[string]CommandBlock, commandBlock *CommandBlock) error {
for _, dep := range commandBlock.Dependencies {
if _, ok := commands[dep]; !ok {
return fmt.Errorf("%w: %s", ErrDependencyNotFound, dep)
}
dependency := commands[dep]
if err := validateDependencies(commands, &dependency); err != nil {
return err
}
}
return nil
}

func executeCommandBlock(commands map[string]CommandBlock, commandBlock *CommandBlock, args ...string) error {

if err := validateDependencies(commands, commandBlock); err != nil {
return err
}

for _, dep := range commandBlock.Dependencies {
if _, ok := commands[dep]; !ok {
return fmt.Errorf("%w: %s", ErrDependencyNotFound, dep)
Expand Down
27 changes: 27 additions & 0 deletions exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,30 @@ func TestExecuteCodeBlock_LauncherNotDefined(t *testing.T) {
t.Errorf("executeCodeBlock() error = %v, wantErr %v", err, wantErr)
}
}

func TestExecuteCodeBlock_DependencyMissing(t *testing.T) {
launchers = map[string]LauncherBlock{"sh": {"sh", "sh"}, "bash": {"sh", "sh"}}

args := []string{}
wantErr := ErrDependencyNotFound
commands := map[string]CommandBlock{}
loadCommands("tests/test_dependency_missing.md", commands)
commandBlock := commands["cmd1"]
output, err := captureOutput(func() error {
return executeCommandBlock(commands, &commandBlock, args...)
})

// This test would output Hello, if the availability of all deps is not validated before execution.
expectedOutput := ""
if output != expectedOutput {
t.Errorf("executeCodeBlock() output = %v, expectedOutput %v", output, expectedOutput)
}

if wantErr != nil {
if !errors.Is(err, wantErr) {
t.Errorf("executeCodeBlock() error = %v, wantErr %v", err, wantErr)
}
} else if err != nil {
t.Errorf("executeCodeBlock() error = %v, wantErr %v", err, wantErr)
}
}
18 changes: 18 additions & 0 deletions tests/test_dependency_missing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## [cmd1](dep1 dep2)

This test would output Hello, if the availability of all deps is not validated before execution.

```sh
echo -n "Hello"
```
## [dep1]()

```sh
echo -n "Hello"
```

## [dep2](dep2.1)

```sh
echo -n "!"
```

0 comments on commit ac905be

Please sign in to comment.