Skip to content

Commit

Permalink
Merge pull request #50 from Jumpaku/test/cli-test
Browse files Browse the repository at this point in the history
CLI testing
  • Loading branch information
Jumpaku authored Aug 3, 2024
2 parents 6537f55 + 6788824 commit 2929a4b
Show file tree
Hide file tree
Showing 188 changed files with 2,288 additions and 10,517 deletions.
17 changes: 16 additions & 1 deletion .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,27 @@ on:
workflow_dispatch:

jobs:
build:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v4
with:
go-version: '1.21.4'
cache-dependency-path: go.sum
- name: check versions and run test
run: make check
test-cli:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v4
with:
go-version: '1.21.4'
- name: check generated CLI behaviour
run: |
set -e
make install
cd cli-test
make cli
make run_test
4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ check: ## Checks version, runs tests.
grep -E '^version: $(VERSION)$$' ./cyamli/cli.yaml
go test ./...

.PHONY: update-test
update-test: ## Checks version, runs tests.
go test ./cyamli/... -update

.PHONY: install
install: ## Install cyamli built in present status.
go generate -v ./...
Expand Down
24 changes: 24 additions & 0 deletions cli-test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.DEFAULT_GOAL := help
.PHONY: help
help: ## Show help
@grep -E '^[a-zA-Z_-]+:.*?##.*$$' $(MAKEFILE_LIST) | \
awk 'BEGIN {FS = ":.*?##"}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'

.PHONY: cli
cli: ## Generates CLIs to be tested
for cli_yaml in cli/*.yaml; do \
group=$$(basename "$${cli_yaml}" .yaml); \
cyamli generate dart < "cli/$${group}.yaml" > "dart/$${group}/cli.g.dart"; \
cyamli generate golang -package=main < "cli/$${group}.yaml" > "golang/$${group}/cli.gen.go"; \
cyamli generate python3 < "cli/$${group}.yaml" > "python3/$${group}/cli_gen.py"; \
done

.PHONY: testcases
testcases: ## Generates testcases
go run ./internal/gen-testcases .

.PHONY: run_test
run_test: ## Run tests
docker compose up --exit-code-from=test-dart --abort-on-container-exit test-dart
docker compose up --exit-code-from=test-golang --abort-on-container-exit test-golang
docker compose up --exit-code-from=test-python3 --abort-on-container-exit test-python3
10 changes: 10 additions & 0 deletions cli-test/cli/root_command_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
options:
-opt-integer:
short: -i
type: integer
-opt-boolean:
short: -b
type: boolean
-opt-string:
short: -s
type: string
9 changes: 9 additions & 0 deletions cli-test/cli/root_command_positional.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
arguments:
- name: arg_integer
type: integer
- name: arg_boolean
type: boolean
- name: arg_string
type: string
- name: arg_variadic
variadic: true
12 changes: 12 additions & 0 deletions cli-test/cli/subcommand_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
subcommands:
sub:
options:
-opt-integer:
short: -i
type: integer
-opt-boolean:
short: -b
type: boolean
-opt-string:
short: -s
type: string
11 changes: 11 additions & 0 deletions cli-test/cli/subcommand_positional.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
subcommands:
sub:
arguments:
- name: arg_integer
type: integer
- name: arg_boolean
type: boolean
- name: arg_string
type: string
- name: arg_variadic
variadic: true
186 changes: 186 additions & 0 deletions cli-test/dart/root_command_options/cli.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions cli-test/dart/root_command_options/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'cli.g.dart';

void main(List<String> args) {
final cli = CLI();
cli.FUNC = (subcommand, input, inputErr) {
if (inputErr != null) {
throw inputErr;
}
print(
"${subcommand.join("-")}_${input?.optOptInteger}_${input?.optOptBoolean}_${input?.optOptString}");
};
run(cli, args);
}
57 changes: 57 additions & 0 deletions cli-test/dart/root_command_options/testcases.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/sh
# Code generated by go run ./cli-test/internal/gen-testcases/... DO NOT EDIT!

run_test() {
message=$1
want=$2
entrypoint=$3
shift 3
got=$( ${entrypoint} $@ )

if [ "${got}" = "${want}" ]; then
echo "OK: ${message}"
else
echo "NG: ${message}"
echo " execution: ${entrypoint} $@"
echo " want: '${want}'"
echo " got: '${got}'"
fi
}


run_test 'root command should handle optional arguments of type integer' \
'_123_false_' \
dart main.dart -opt-integer=123

run_test 'root command should handle optional arguments of type boolean' \
'_0_true_' \
dart main.dart -opt-boolean=true

run_test 'root command should handle optional arguments of type boolean' \
'_0_false_abc' \
dart main.dart -opt-string=abc

run_test 'root command should handle optional arguments of type integer' \
'_123_false_' \
dart main.dart -i=123

run_test 'root command should handle short optional arguments of type boolean' \
'_0_true_' \
dart main.dart -b=true

run_test 'root command should handle short optional arguments of type boolean' \
'_0_false_abc' \
dart main.dart -s=abc

run_test 'root command should handle default optional argument values' \
'_0_false_' \
dart main.dart

run_test 'root command should handle optional arguments of type boolean without value' \
'_0_true_' \
dart main.dart -opt-boolean

run_test 'root command should handle short optional arguments of type boolean without value' \
'_0_true_' \
dart main.dart -b

Loading

0 comments on commit 2929a4b

Please sign in to comment.