From c4b13ef326d5c4af18fe2dabe16a7a55e9f5a8c1 Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Fri, 22 Dec 2023 10:54:19 +0000 Subject: [PATCH 1/2] add specs for default_string (arg and flag) --- .github/workflows/test.yml | 4 ++-- spec/bashly/script/argument_spec.rb | 26 ++++++++++++++++++++++++++ spec/bashly/script/flag_spec.rb | 26 ++++++++++++++++++++++++++ spec/fixtures/script/arguments.yml | 15 +++++++++++++++ spec/fixtures/script/flags.yml | 20 +++++++++++++++++++- 5 files changed, 88 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 644f2408..d15e87e2 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -47,7 +47,7 @@ jobs: run: bundle exec rspec static_analysis: - name: Static analysis of Example files + name: Example files static analysis runs-on: ubuntu-latest @@ -85,7 +85,7 @@ jobs: run: bundle exec run shfmt json_schema: - name: Validate JSON schemas + name: JSON schemas validation runs-on: ubuntu-latest diff --git a/spec/bashly/script/argument_spec.rb b/spec/bashly/script/argument_spec.rb index 3f810236..8d1bb973 100644 --- a/spec/bashly/script/argument_spec.rb +++ b/spec/bashly/script/argument_spec.rb @@ -6,6 +6,32 @@ let(:fixture) { :basic_argument } + describe '#default_string' do + context 'when default is an array' do + let(:fixture) { :default_array } + + it 'returns a shell-escaped string suitable to be shell array source' do + expect(subject.default_string).to eq 'spaced\\ one two' + end + end + + context 'when default is string and repeatable is false' do + let(:fixture) { :default_string } + + it 'returns it as is' do + expect(subject.default_string).to eq 'spaced one' + end + end + + context 'when default is string and repeatable is true' do + let(:fixture) { :default_string_with_repeatable } + + it 'returns a single string' do + expect(subject.default_string).to eq 'spaced\\ one' + end + end + end + describe '#usage_string' do it 'returns a string suitable to be used as a usage pattern' do expect(subject.usage_string).to eq '[FILE]' diff --git a/spec/bashly/script/flag_spec.rb b/spec/bashly/script/flag_spec.rb index 108acc6a..90b7b216 100644 --- a/spec/bashly/script/flag_spec.rb +++ b/spec/bashly/script/flag_spec.rb @@ -30,6 +30,32 @@ end end + describe '#default_string' do + context 'when default is an array' do + let(:fixture) { :default_array } + + it 'returns a shell-escaped string suitable to be shell array source' do + expect(subject.default_string).to eq 'spaced\\ one two' + end + end + + context 'when default is string and repeatable is false' do + let(:fixture) { :default_string } + + it 'returns it as is' do + expect(subject.default_string).to eq 'spaced one' + end + end + + context 'when default is string and repeatable is true' do + let(:fixture) { :default_string_with_repeatable } + + it 'returns a single string' do + expect(subject.default_string).to eq 'spaced\\ one' + end + end + end + describe '#name' do context 'with both short and long options' do it 'returns the long option' do diff --git a/spec/fixtures/script/arguments.yml b/spec/fixtures/script/arguments.yml index 1f2b6ef5..67f0a3a9 100644 --- a/spec/fixtures/script/arguments.yml +++ b/spec/fixtures/script/arguments.yml @@ -8,3 +8,18 @@ :repeatable: name: file repeatable: true + +:default_string: + name: file + repeatable: false + default: spaced one + +:default_array: + name: file + repeatable: true + default: [spaced one, two] + +:default_string_with_repeatable: + name: file + repeatable: true + default: spaced one diff --git a/spec/fixtures/script/flags.yml b/spec/fixtures/script/flags.yml index e1766036..e0de14e6 100644 --- a/spec/fixtures/script/flags.yml +++ b/spec/fixtures/script/flags.yml @@ -25,4 +25,22 @@ :completions_completions: long: --path short: -p - completions: [, README.md] \ No newline at end of file + completions: [, README.md] + +:default_string: + long: --file + arg: path + repeatable: false + default: spaced one + +:default_array: + long: --file + arg: path + repeatable: true + default: [spaced one, two] + +:default_string_with_repeatable: + long: --file + arg: path + repeatable: true + default: spaced one From 2c75ef0758bde29464a741691cf41e8cd2fc27fb Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Fri, 22 Dec 2023 11:37:30 +0000 Subject: [PATCH 2/2] simplify environment variable schema --- schemas/bashly.json | 130 ++++++++++++-------------------------- support/schema/bashly.yml | 76 +++++++--------------- 2 files changed, 63 insertions(+), 143 deletions(-) diff --git a/schemas/bashly.json b/schemas/bashly.json index c48b105e..51192fc9 100644 --- a/schemas/bashly.json +++ b/schemas/bashly.json @@ -281,6 +281,44 @@ }, "additionalProperties": false }, + "environment-variable": { + "title": "environment variable", + "description": "An environment variable of the current application\nhttps://bashly.dannyb.co/configuration/environment-variable/#environment-variable", + "type": "object", + "required": [ + "name" + ], + "properties": { + "name": { + "$ref": "#/definitions/environment-variables-name-property" + }, + "help": { + "$ref": "#/definitions/environment-variables-help-property" + }, + "private": { + "$ref": "#/definitions/environment-variables-private-property" + }, + "required": { + "$ref": "#/definitions/environment-variables-required-property" + }, + "allowed": { + "$ref": "#/definitions/environment-variables-allowed-property" + }, + "default": { + "$ref": "#/definitions/environment-variables-default-property" + } + }, + "patternProperties": { + "^x_.": { + "title": "custom property", + "description": "A custom property of any type", + "examples": [ + "Anything" + ] + } + }, + "additionalProperties": false + }, "name-property": { "title": "name", "description": "A name of the current script or sub-command\nhttps://bashly.dannyb.co/configuration/command/#name", @@ -442,97 +480,7 @@ "type": "array", "uniqueItems": true, "items": { - "title": "environment variable", - "description": "An environment variable of the current application\nhttps://bashly.dannyb.co/configuration/environment-variable/#environment-variable", - "type": "object", - "required": [ - "name" - ], - "properties": { - "name": { - "$ref": "#/definitions/environment-variables-name-property" - }, - "help": { - "$ref": "#/definitions/environment-variables-help-property" - }, - "default": { - "$ref": "#/definitions/environment-variables-default-property" - }, - "private": { - "$ref": "#/definitions/environment-variables-private-property" - }, - "required": { - "$ref": "#/definitions/environment-variables-required-property" - }, - "allowed": { - "$ref": "#/definitions/environment-variables-allowed-property" - } - }, - "if": { - "properties": { - "required": { - "const": false - } - } - }, - "then": { - "properties": { - "name": { - "$ref": "#/definitions/environment-variables-name-property" - }, - "help": { - "$ref": "#/definitions/environment-variables-help-property" - }, - "default": { - "$ref": "#/definitions/environment-variables-default-property" - }, - "private": { - "$ref": "#/definitions/environment-variables-private-property" - }, - "required": { - "$ref": "#/definitions/environment-variables-required-property" - }, - "allowed": { - "$ref": "#/definitions/environment-variables-allowed-property" - } - }, - "patternProperties": { - "^x_.": { - "title": "custom property", - "description": "A custom property of any type", - "examples": [ - "Anything" - ] - } - }, - "additionalProperties": false - }, - "else": { - "properties": { - "name": { - "$ref": "#/definitions/environment-variables-name-property" - }, - "help": { - "$ref": "#/definitions/environment-variables-help-property" - }, - "private": { - "$ref": "#/definitions/environment-variables-private-property" - }, - "required": { - "$ref": "#/definitions/environment-variables-required-property" - } - }, - "patternProperties": { - "^x_.": { - "title": "custom property", - "description": "A custom property of any type", - "examples": [ - "Anything" - ] - } - }, - "additionalProperties": false - } + "$ref": "#/definitions/environment-variable" } }, "examples-property": { diff --git a/support/schema/bashly.yml b/support/schema/bashly.yml index 53d401ab..f831dd4e 100644 --- a/support/schema/bashly.yml +++ b/support/schema/bashly.yml @@ -245,6 +245,29 @@ definitions: const: true patternProperties: *custom-properties additionalProperties: false + environment-variable: + title: environment variable + description: |- + An environment variable of the current application + https://bashly.dannyb.co/configuration/environment-variable/#environment-variable + type: object + required: + - name + properties: + name: + $ref: '#/definitions/environment-variables-name-property' + help: + $ref: '#/definitions/environment-variables-help-property' + private: + $ref: '#/definitions/environment-variables-private-property' + required: + $ref: '#/definitions/environment-variables-required-property' + allowed: + $ref: '#/definitions/environment-variables-allowed-property' + default: + $ref: '#/definitions/environment-variables-default-property' + patternProperties: *custom-properties + additionalProperties: false name-property: title: name description: |- @@ -399,58 +422,7 @@ definitions: type: array uniqueItems: true items: - title: environment variable - description: |- - An environment variable of the current application - https://bashly.dannyb.co/configuration/environment-variable/#environment-variable - type: object - required: - - name - properties: - name: - $ref: '#/definitions/environment-variables-name-property' - help: - $ref: '#/definitions/environment-variables-help-property' - default: - $ref: '#/definitions/environment-variables-default-property' - private: - $ref: '#/definitions/environment-variables-private-property' - required: - $ref: '#/definitions/environment-variables-required-property' - allowed: - $ref: '#/definitions/environment-variables-allowed-property' - if: - properties: - required: - const: false - then: - properties: - name: - $ref: '#/definitions/environment-variables-name-property' - help: - $ref: '#/definitions/environment-variables-help-property' - default: - $ref: '#/definitions/environment-variables-default-property' - private: - $ref: '#/definitions/environment-variables-private-property' - required: - $ref: '#/definitions/environment-variables-required-property' - allowed: - $ref: '#/definitions/environment-variables-allowed-property' - patternProperties: *custom-properties - additionalProperties: false - else: - properties: - name: - $ref: '#/definitions/environment-variables-name-property' - help: - $ref: '#/definitions/environment-variables-help-property' - private: - $ref: '#/definitions/environment-variables-private-property' - required: - $ref: '#/definitions/environment-variables-required-property' - patternProperties: *custom-properties - additionalProperties: false + $ref: '#/definitions/environment-variable' examples-property: title: examples oneOf: