Skip to content

Commit

Permalink
Merge pull request #121 from ahoy-cli/optional-imports
Browse files Browse the repository at this point in the history
Add tests for the optional imports feature ( #119 )
  • Loading branch information
ocean authored Sep 5, 2024
2 parents 9fa918b + bf8ad89 commit cb468d7
Show file tree
Hide file tree
Showing 15 changed files with 236 additions and 68 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
version: 2.1
orbs:
go: circleci/go@1.10.0
go: circleci/go@1.11.0

jobs:
build:
executor:
name: go/default
tag: '1.20'
tag: '1.22'
working_directory: /home/circleci/go/src/github.com/ahoy-cli/ahoy
steps:
- checkout
Expand Down
15 changes: 9 additions & 6 deletions ahoy.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ func getSubCommands(includes []string) []cli.Command {
if len(include) == 0 {
continue
}
if include[0] != "/"[0] || include[0] != "~"[0] {
// If the include path is not absolute or a home directory path,
// prepend the source directory to make it relative to the config file.
if !strings.HasPrefix(include, "/") && !strings.HasPrefix(include, "~") {
include = filepath.Join(AhoyConf.srcDir, include)
}
if _, err := os.Stat(include); err != nil {
Expand Down Expand Up @@ -186,7 +188,7 @@ func getCommands(config Config) []cli.Command {
logger("fatal", "Command ["+name+"] has neither 'cmd' or 'imports' set. Check your yaml file.")
}

// Check that a command has 'cmd' OR 'imports' set.
// Check that a command has 'cmd' AND 'imports' set.
if cmd.Cmd != "" && cmd.Imports != nil {
logger("fatal", "Command ["+name+"] has both 'cmd' and 'imports' set, but only one is allowed. Check your yaml file.")
}
Expand Down Expand Up @@ -262,7 +264,7 @@ func getCommands(config Config) []cli.Command {
newCmd.Subcommands = subCommands
}

// log.Println("found command: ", name, " > ", cmd.Cmd)
// log.Println("Source file:", sourcefile, "- found command:", name, ">", cmd.Cmd)
exportCmds = append(exportCmds, newCmd)
}

Expand Down Expand Up @@ -376,11 +378,12 @@ func NoArgsAction(c *cli.Context) {
}

if !c.Bool("help") || !c.Bool("version") {
logger("fatal", "Missing flag or argument.")
logger("warn", "Missing flag or argument.")
os.Exit(1)
}

// Looks like we never reach here.
fmt.Println("ERROR: NoArg Action ")
// Exit gracefully if we get to here.
os.Exit(0)
}

// BeforeCommand runs before every command so arguments or flags must be passed
Expand Down
26 changes: 13 additions & 13 deletions examples/docker.ahoy.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
ahoyapi: v2
commands:
up:
cmd: "docker-compose up -d"
usage: Start the docker-compose containers.
cmd: "docker compose up -d"
usage: Start the Docker Compose containers.
proxy-up:
cmd: "docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy"
usage: Run the nginx-proxy container
stop:
cmd: "docker-compose stop"
usage: Stop the docker-compose containers (non-destructive).
cmd: "docker compose stop"
usage: Stop the Docker Compose containers (non-destructive).
ps:
cmd: "docker-compose ps"
usage: List the running docker-compose containers.
cmd: "docker compose ps"
usage: List the running Docker Compose containers.
ip:
cmd: "docker-machine ip default"
usage: Show the ip address f the default docker machine VM
reset:
cmd: "docker-compose stop && docker-compose rm && ahoy up"
usage: Start the docker compose-containers.
cmd: "docker compose stop && docker compose rm && ahoy up"
usage: Start the Docker Compose containers.
exec:
cmd: docker exec -it $(docker-compose ps -q cli) bash -c "$@"
usage: run a command in the docker-compose cli service container.
cmd: docker exec -it $(docker compose ps -q cli) bash -c "$@"
usage: run a command in the Docker Compose cli service container.
mysql:
cmd: "docker exec -it $(docker-compose ps -q cli) bash -c 'mysql -u$DB_ENV_MYSQL_USER -p$DB_ENV_MYSQL_PASSWORD -h$DB_PORT_3306_TCP_ADDR $DB_ENV_MYSQL_DATABASE'"
cmd: "docker exec -it $(docker compose ps -q cli) bash -c 'mysql -u$DB_ENV_MYSQL_USER -p$DB_ENV_MYSQL_PASSWORD -h$DB_PORT_3306_TCP_ADDR $DB_ENV_MYSQL_DATABASE'"
usage: Connect to the default mysql database.
mysql-import:
cmd: "docker exec -i $(docker-compose ps -q cli) bash -c 'mysql -u$DB_ENV_MYSQL_USER -p$DB_ENV_MYSQL_PASSWORD -h$DB_PORT_3306_TCP_ADDR $DB_ENV_MYSQL_DATABASE'"
cmd: "docker exec -i $(docker compose ps -q cli) bash -c 'mysql -u$DB_ENV_MYSQL_USER -p$DB_ENV_MYSQL_PASSWORD -h$DB_PORT_3306_TCP_ADDR $DB_ENV_MYSQL_DATABASE'"
usage: Pipe in a sql file. `ahoy mysql-import < backups/live.sql`
mysql-dump:
cmd: "docker exec -it $(docker-compose ps -q cli) bash -c 'mysqldump -u$DB_ENV_MYSQL_USER -p$DB_ENV_MYSQL_PASSWORD -h$DB_PORT_3306_TCP_ADDR $DB_ENV_MYSQL_DATABASE'"
cmd: "docker exec -it $(docker compose ps -q cli) bash -c 'mysqldump -u$DB_ENV_MYSQL_USER -p$DB_ENV_MYSQL_PASSWORD -h$DB_PORT_3306_TCP_ADDR $DB_ENV_MYSQL_DATABASE'"
usage: Dump data out into a file. `ahoy mysql-import > backups/local.sql`
override-example:
cmd: echo "Override me"
30 changes: 15 additions & 15 deletions examples/examples.ahoy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,42 @@ commands:
usage: Stop the vagrant box if one exists.

ps:
cmd: docker-compose ps "$@"
usage: List the running docker-compose containers.
cmd: docker compose ps "$@"
usage: List the running Docker Compose containers.

start:
cmd: docker-compose start "$@"
usage: Start the docker-compose containers.
cmd: docker compose start "$@"
usage: Start the Docker Compose containers.

stop:
cmd: docker-compose stop "$@"
usage: Stop the docker-compose containers.
cmd: docker compose stop "$@"
usage: Stop the Docker Compose containers.

restart:
cmd: docker-compose restart "$@"
usage: Restart the docker-compose containers.
cmd: docker compose restart "$@"
usage: Restart the Docker Compose containers.

drush:
cmd: docker-compose run cli drush --root=docroot "$@"
cmd: docker compose run cli drush --root=docroot "$@"
usage: Run drush commands in the cli service container.

bash:
cmd: docker-compose run "$1" bash
cmd: docker compose run "$1" bash
usage: Start a shell in the container (like ssh without actual ssh).

sqlc:
cmd: "docker-compose run cli drush --root=docroot sqlc"
cmd: "docker compose run cli drush --root=docroot sqlc"
usage: Connect to the default mysql database. Supports piping of data into the command.

behat:
cmd: docker-compose run cli bash -c "cd docroot/test && composer install --prefer-source --no-interaction && bin/behat -p docker $@"
cmd: docker compose run cli bash -c "cd docroot/test && composer install --prefer-source --no-interaction && bin/behat -p docker $@"
usage: Run the behat tests within the container.

behat-init:
cmd: 'docker-compose run cli bash -c "cd docroot/test && composer install --prefer-source --no-interaction"'
cmd: 'docker compose run cli bash -c "cd docroot/test && composer install --prefer-source --no-interaction"'
usage: Use composer to install behat dependencies.

#This command overrides the up command in the docker.ahoy.yml to test the "last in wins" feature of ahoy
up:
cmd: "docker-compose up -d"
usage: Start the docker-compose containers.
cmd: "docker compose up -d"
usage: Start the Docker Compose containers.
31 changes: 14 additions & 17 deletions testdata/docker.ahoy.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
ahoyapi: v2
commands:
up:
cmd: "docker-compose up -d"
usage: Start the docker-compose containers.
cmd: "docker compose up -d"
usage: Start the Docker Compose containers.
proxy-up:
cmd: "docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy"
usage: Run the nginx-proxy container
usage: Run the nginx-proxy container.
stop:
cmd: "docker-compose stop"
usage: Stop the docker-compose containers (non-destructive).
cmd: "docker compose stop"
usage: Stop the Docker Compose containers (non-destructive).
ps:
cmd: "docker-compose ps"
usage: List the running docker-compose containers.
ip:
cmd: "docker-machine ip default"
usage: Show the ip address f the default docker machine VM
cmd: "docker compose ps"
usage: List the running Docker Compose containers.
reset:
cmd: "docker-compose stop && docker-compose rm && ahoy up"
usage: Start the docker compose-containers.
cmd: "docker compose stop && docker compose rm && ahoy up"
usage: Start the Docker Compose containers.
exec:
cmd: docker exec -it $(docker-compose ps -q cli) bash -c "$@"
usage: run a command in the docker-compose cli service container.
cmd: docker exec -it $(docker compose ps -q cli) bash -c "$@"
usage: Run a command in the Docker Compose cli service container.
mysql:
cmd: "docker exec -it $(docker-compose ps -q cli) bash -c 'mysql -u$DB_ENV_MYSQL_USER -p$DB_ENV_MYSQL_PASSWORD -h$DB_PORT_3306_TCP_ADDR $DB_ENV_MYSQL_DATABASE'"
cmd: "docker exec -it $(docker compose ps -q cli) bash -c 'mysql -u$DB_ENV_MYSQL_USER -p$DB_ENV_MYSQL_PASSWORD -h$DB_PORT_3306_TCP_ADDR $DB_ENV_MYSQL_DATABASE'"
usage: Connect to the default mysql database.
mysql-import:
cmd: "docker exec -i $(docker-compose ps -q cli) bash -c 'mysql -u$DB_ENV_MYSQL_USER -p$DB_ENV_MYSQL_PASSWORD -h$DB_PORT_3306_TCP_ADDR $DB_ENV_MYSQL_DATABASE'"
cmd: "docker exec -i $(docker compose ps -q cli) bash -c 'mysql -u$DB_ENV_MYSQL_USER -p$DB_ENV_MYSQL_PASSWORD -h$DB_PORT_3306_TCP_ADDR $DB_ENV_MYSQL_DATABASE'"
usage: Pipe in a sql file. `ahoy mysql-import < backups/live.sql`
mysql-dump:
cmd: "docker exec -it $(docker-compose ps -q cli) bash -c 'mysqldump -u$DB_ENV_MYSQL_USER -p$DB_ENV_MYSQL_PASSWORD -h$DB_PORT_3306_TCP_ADDR $DB_ENV_MYSQL_DATABASE'"
cmd: "docker exec -it $(docker compose ps -q cli) bash -c 'mysqldump -u$DB_ENV_MYSQL_USER -p$DB_ENV_MYSQL_PASSWORD -h$DB_PORT_3306_TCP_ADDR $DB_ENV_MYSQL_DATABASE'"
usage: Dump data out into a file. `ahoy mysql-import > backups/local.sql`
override-example:
cmd: echo "Override me"
2 changes: 1 addition & 1 deletion testdata/missing-imports.ahoy.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ahoyapi: v2
commands:
missing-imports:
usage: "This item has imports set, but it doesn't exit."
usage: "This item has imports set, but it doesn't exist."
imports:
- "bogus.ahoy.yml"
6 changes: 6 additions & 0 deletions testdata/non-optional-command.ahoy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ahoyapi: v2
commands:
non-optional-cmd:
usage: This command is not optional
imports:
- non-existent-file.yml
10 changes: 10 additions & 0 deletions testdata/optional-command.ahoy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
ahoyapi: v2
commands:
optional-cmd:
usage: These imports are optional
imports:
- testdata/non-existent-file.yml
optional: true
regular-cmd:
usage: This is a regular command
cmd: echo "This is a regular command"
2 changes: 1 addition & 1 deletion tests/cross-compile.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
load 'test_helpers/bats-support/load'
load 'test_helpers/bats-assert/load'

@test "test cross compilation command lifecycle" {
@test "Test cross compilation command lifecycle" {
run make clean
assert_success
assert_output --partial 'rm -vRf ./builds/ahoy-bin-*'
Expand Down
4 changes: 2 additions & 2 deletions tests/entrypoint.bats
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bats

@test "override bash entrypoint to add additional flags" {
@test "Override bash entrypoint to add additional flags" {
run ./ahoy -f testdata/entrypoint-bash.ahoy.yml echo something
[ $status -eq 0 ]
echo "$output"
Expand All @@ -10,7 +10,7 @@

}

@test "override bash entrypoint to use php instead" {
@test "Override bash entrypoint to use PHP instead" {
run ./ahoy -f testdata/entrypoint-php.ahoy.yml echo something
[ $status -eq 0 ]
echo "$output"
Expand Down
4 changes: 2 additions & 2 deletions tests/flags.bats
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env bats

@test "get the version of ahoy with --version" {
@test "Get the version of ahoy with --version" {
run ./ahoy -f testdata/simple.ahoy.yml --version
[ $status -eq 0 ]
[ $(expr "$output" : "^v?[0-9.]\.[0-9.]\.[0-9.](\S*)?") -eq 0 ]
}

@test "get help instead of running a command with --help" {
@test "Get help instead of running a command with --help" {
result="$(./ahoy -f testdata/simple.ahoy.yml --help echo something)"
[ "$result" != "something" ]
}
10 changes: 5 additions & 5 deletions tests/no-ahoy-file.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ teardown() {
mv tmp.ahoy.yml .ahoy.yml
}

@test "run ahoy without a command and without a .ahoy.yml file" {
@test "Run ahoy without a command and without an .ahoy.yml file" {
run ./ahoy
[ $status -eq 1 ]
[ "${lines[-2]}" == "[error] No .ahoy.yml found. You can use 'ahoy init' to download an example." ]
[ "${lines[-1]}" == "[fatal] Missing flag or argument." ]
[ "${lines[-1]}" == "[warn] Missing flag or argument." ]
}

@test "run an ahoy command without a .ahoy.yml file" {
@test "Run an ahoy command without an .ahoy.yml file" {
run ./ahoy something
[ "$output" == "[fatal] Command not found for 'something'" ]
}

@test "run ahoy init without a .ahoy.yml file" {
@test "Run ahoy init without an .ahoy.yml file" {
run ./ahoy init
[ "${lines[-1]}" == "Example .ahoy.yml downloaded to the current directory. You can customize it to suit your needs!" ]
}

@test "run ahoy init with a existing .ahoy.yml file in the current directory" {
@test "Run ahoy init with an existing .ahoy.yml file in the current directory" {
cp tmp.ahoy.yml .ahoy.yml
run ./ahoy init --force
[ "${lines[0]}" == "Warning: '--force' parameter passed, overwriting .ahoy.yml in current directory." ]
Expand Down
42 changes: 42 additions & 0 deletions tests/optional-commands.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env bats

load 'test_helpers/bats-support/load'
load 'test_helpers/bats-assert/load'

@test "Optional import not found doesn't cause error" {
# Run ahoy without arguments (which typically lists available commands)
run ./ahoy -f testdata/optional-command.ahoy.yml

# Check that the optional command is not listed
[[ ! "$output" =~ "optional-cmd" ]]

# Check that the regular command is listed
[[ "$output" =~ "regular-cmd" ]]

# Check that a standard "Missing argument" error is shown
[[ "$output" =~ "Missing flag or argument" ]]

# Check that the command exited with an error
[ $status -eq 1 ]

# Try to run the optional command (it should fail gracefully)
run ./ahoy -f testdata/optional-command.ahoy.yml optional-cmd
[ $status -eq 1 ]
[[ "$output" =~ "Command not found for 'optional-cmd'" ]]

# Run the regular command (it should work)
run ./ahoy -f testdata/optional-command.ahoy.yml regular-cmd
[ $status -eq 0 ]
[[ "$output" = "This is a regular command" ]]
}

@test "Non-optional command with missing imports causes error" {
# Run ahoy without arguments
run ./ahoy -f testdata/non-optional-command.ahoy.yml

# Check that the command failed
[ $status -eq 1 ]

# Check for the appropriate error message
[[ "$output" =~ "Command [non-optional-cmd] has 'imports' set, but no commands were found" ]]
}
8 changes: 4 additions & 4 deletions tests/simple.bats
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#!/usr/bin/env bats

@test "display help text and fatal error when no arguments are passed." {
@test "Display help text and fatal error when no arguments are passed." {
run ./ahoy -f testdata/simple.ahoy.yml
# Should throw an error.
[ $status -ne 0 ]
echo "$output"
[ "${#lines[@]}" -gt 10 ]
[ "${lines[-1]}" == "[fatal] Missing flag or argument." ]
[ "${lines[-1]}" == "[warn] Missing flag or argument." ]
}

@test "run a simple ahoy command: echo" {
@test "Run a simple ahoy command: echo" {
result="$(./ahoy -f testdata/simple.ahoy.yml echo something)"
[ "$result" == "something" ]
}

@test "run a simple ahoy command (ls -a) with an extra parameter (-l)" {
@test "Run a simple ahoy command (ls -a) with an extra parameter (-l)" {
run ./ahoy -f testdata/simple.ahoy.yml list -- -l
[ "${#lines[@]}" -gt 13 ]
}
Loading

0 comments on commit cb468d7

Please sign in to comment.