diff --git a/examples/render-mandoc/docs/download.1 b/examples/render-mandoc/docs/download.1 index 104f3572..acad50a9 100644 --- a/examples/render-mandoc/docs/download.1 +++ b/examples/render-mandoc/docs/download.1 @@ -14,7 +14,7 @@ . ftr VB CB . ftr VBI CBI .\} -.TH "download" "1" "September 2023" "Version 0.1.0" "Sample application" +.TH "download" "1" "October 2023" "Version 0.1.0" "Sample application" .hy .SH NAME .PP diff --git a/examples/render-mandoc/docs/download.md b/examples/render-mandoc/docs/download.md index 94cae278..df151e66 100644 --- a/examples/render-mandoc/docs/download.md +++ b/examples/render-mandoc/docs/download.md @@ -1,6 +1,6 @@ % download(1) Version 0.1.0 | Sample application % Lana Lang -% September 2023 +% October 2023 NAME ================================================== diff --git a/lib/bashly.rb b/lib/bashly.rb index 470ae2fc..9b2ec75a 100644 --- a/lib/bashly.rb +++ b/lib/bashly.rb @@ -15,6 +15,7 @@ module Bashly autoload :ConfigValidator, 'bashly/config_validator' autoload :Library, 'bashly/library' autoload :LibrarySource, 'bashly/library_source' + autoload :LibrarySourceConfig, 'bashly/library_source_config' autoload :MessageStrings, 'bashly/message_strings' autoload :RenderContext, 'bashly/render_context' autoload :RenderSource, 'bashly/render_source' diff --git a/lib/bashly/library_source.rb b/lib/bashly/library_source.rb index 662e8f45..1185cebf 100644 --- a/lib/bashly/library_source.rb +++ b/lib/bashly/library_source.rb @@ -15,7 +15,7 @@ def git? end def config - @config ||= YAML.load_file config_path + @config ||= LibrarySourceConfig.new(config_path).validated_data end def libraries diff --git a/lib/bashly/library_source_config.rb b/lib/bashly/library_source_config.rb new file mode 100644 index 00000000..53f381fa --- /dev/null +++ b/lib/bashly/library_source_config.rb @@ -0,0 +1,48 @@ +module Bashly + class LibrarySourceConfig + include ValidationHelpers + + attr_reader :path + + def initialize(path) + @path = path + end + + def data + @data ||= YAML.load_file path + end + + def validated_data + validate + data + end + + def validate + assert_root path, data + end + + private + + def assert_root(path, value) + assert_hash path, value + data.each { |id, spec| assert_lib "[#{path}] #{id}", spec } + end + + def assert_lib(key, value) + assert_string "#{key}.help", value['help'] + + assert_optional_string "#{key}.usage", value['usage'] + assert_optional_string "#{key}.handler", value['handler'] + assert_optional_string "#{key}.post_install_message", value['post_install_message'] + + return if value['handler'] + + assert_array "#{key}.files", value['files'], of: :filespec + end + + def assert_filespec(key, value) + assert_string "#{key}.source", value['source'] + assert_string "#{key}.target", value['target'] + end + end +end diff --git a/spec/approvals/examples/render-mandoc b/spec/approvals/examples/render-mandoc index 141c88f3..c44684e0 100644 --- a/spec/approvals/examples/render-mandoc +++ b/spec/approvals/examples/render-mandoc @@ -44,4 +44,4 @@ ISSUE TRACKER AUTHORS Lana Lang. -Version 0.1.0 September 2023 download(1) +Version 0.1.0 October 2023 download(1) diff --git a/spec/approvals/libraries/render/mandoc/render-1-download.1 b/spec/approvals/libraries/render/mandoc/render-1-download.1 index 08b2b76b..0c3b39c3 100644 --- a/spec/approvals/libraries/render/mandoc/render-1-download.1 +++ b/spec/approvals/libraries/render/mandoc/render-1-download.1 @@ -29,4 +29,4 @@ EXAMPLES download example.com ./output -f -Version 0.1.0 MONTH YEAR download(1) +Version 0.1.0 MONTH YEAR download(1) diff --git a/spec/approvals/libraries_validation/files_array b/spec/approvals/libraries_validation/files_array new file mode 100644 index 00000000..f78264a3 --- /dev/null +++ b/spec/approvals/libraries_validation/files_array @@ -0,0 +1 @@ +# \ No newline at end of file diff --git a/spec/approvals/libraries_validation/files_source b/spec/approvals/libraries_validation/files_source new file mode 100644 index 00000000..21f98c90 --- /dev/null +++ b/spec/approvals/libraries_validation/files_source @@ -0,0 +1 @@ +# \ No newline at end of file diff --git a/spec/approvals/libraries_validation/files_target b/spec/approvals/libraries_validation/files_target new file mode 100644 index 00000000..67580069 --- /dev/null +++ b/spec/approvals/libraries_validation/files_target @@ -0,0 +1 @@ +# \ No newline at end of file diff --git a/spec/approvals/libraries_validation/handler_string b/spec/approvals/libraries_validation/handler_string new file mode 100644 index 00000000..4fb9d1db --- /dev/null +++ b/spec/approvals/libraries_validation/handler_string @@ -0,0 +1 @@ +# \ No newline at end of file diff --git a/spec/approvals/libraries_validation/help_string b/spec/approvals/libraries_validation/help_string new file mode 100644 index 00000000..8d40fb91 --- /dev/null +++ b/spec/approvals/libraries_validation/help_string @@ -0,0 +1 @@ +# \ No newline at end of file diff --git a/spec/approvals/libraries_validation/message_string b/spec/approvals/libraries_validation/message_string new file mode 100644 index 00000000..f9ecbea5 --- /dev/null +++ b/spec/approvals/libraries_validation/message_string @@ -0,0 +1 @@ +# \ No newline at end of file diff --git a/spec/approvals/libraries_validation/root_hash b/spec/approvals/libraries_validation/root_hash new file mode 100644 index 00000000..bf1c0a67 --- /dev/null +++ b/spec/approvals/libraries_validation/root_hash @@ -0,0 +1 @@ +# \ No newline at end of file diff --git a/spec/approvals/libraries_validation/usage_string b/spec/approvals/libraries_validation/usage_string new file mode 100644 index 00000000..877e9c1e --- /dev/null +++ b/spec/approvals/libraries_validation/usage_string @@ -0,0 +1 @@ +# \ No newline at end of file diff --git a/spec/bashly/config_validator_spec.rb b/spec/bashly/config_validator_spec.rb index 7e88096c..9d091fea 100644 --- a/spec/bashly/config_validator_spec.rb +++ b/spec/bashly/config_validator_spec.rb @@ -1,14 +1,13 @@ describe ConfigValidator do fixtures = load_fixture 'script/validations' - subject { described_class.new options } describe '#validate' do fixtures.each do |fixture, options| - context "with :#{fixture}" do - let(:options) { options } + validator = described_class.new options + context "with :#{fixture}" do it 'raises an error' do - expect { subject.validate }.to raise_approval("validations/#{fixture}") + expect { validator.validate }.to raise_approval("validations/#{fixture}") end end end diff --git a/spec/bashly/library_source_config_spec.rb b/spec/bashly/library_source_config_spec.rb new file mode 100644 index 00000000..9b4739e5 --- /dev/null +++ b/spec/bashly/library_source_config_spec.rb @@ -0,0 +1,26 @@ +describe LibrarySourceConfig do + fixtures = Dir['spec/fixtures/libraries_errors/*.yml'] + subject { described_class.new path } + + let(:path) { 'spec/fixtures/libraries/libraries.yml' } + + describe '#validated_data' do + it 'returns the data after validating it' do + expect(subject).to receive(:validate) + expect(subject.validated_data).to eq subject.data + end + end + + describe '#validate' do + fixtures.each do |path| + fixture_name = File.basename path, '.yml' + config = described_class.new path + + context "with #{fixture_name}.yml" do + it 'raises an error' do + expect { config.validate }.to raise_approval("libraries_validation/#{fixture_name}") + end + end + end + end +end diff --git a/spec/fixtures/libraries_validation/files_array.yml b/spec/fixtures/libraries_validation/files_array.yml new file mode 100644 index 00000000..998fd18c --- /dev/null +++ b/spec/fixtures/libraries_validation/files_array.yml @@ -0,0 +1,4 @@ +# error: files must be an array +database: + help: Help text + files: one_file.txt diff --git a/spec/fixtures/libraries_validation/files_source.yml b/spec/fixtures/libraries_validation/files_source.yml new file mode 100644 index 00000000..a1532f5d --- /dev/null +++ b/spec/fixtures/libraries_validation/files_source.yml @@ -0,0 +1,6 @@ +# error: files must have a source +database: + help: Help text + files: + - target: some_target + diff --git a/spec/fixtures/libraries_validation/files_target.yml b/spec/fixtures/libraries_validation/files_target.yml new file mode 100644 index 00000000..f690b7e9 --- /dev/null +++ b/spec/fixtures/libraries_validation/files_target.yml @@ -0,0 +1,6 @@ +# error: files must have a target +database: + help: Help text + files: + - source: some_source + diff --git a/spec/fixtures/libraries_validation/handler_string.yml b/spec/fixtures/libraries_validation/handler_string.yml new file mode 100644 index 00000000..85266e93 --- /dev/null +++ b/spec/fixtures/libraries_validation/handler_string.yml @@ -0,0 +1,7 @@ +# error: handler must be a string (optional) +database: + help: Help text + handler: 1 + files: + - source: "database/database.sh" + target: "%{user_lib_dir}/database.%{user_ext}" diff --git a/spec/fixtures/libraries_validation/help_string.yml b/spec/fixtures/libraries_validation/help_string.yml new file mode 100644 index 00000000..4d2ae109 --- /dev/null +++ b/spec/fixtures/libraries_validation/help_string.yml @@ -0,0 +1,6 @@ +# error: help must be a string +database: + help: 1 + files: + - source: "database/database.sh" + target: "%{user_lib_dir}/database.%{user_ext}" diff --git a/spec/fixtures/libraries_validation/message_string.yml b/spec/fixtures/libraries_validation/message_string.yml new file mode 100644 index 00000000..0ab1145d --- /dev/null +++ b/spec/fixtures/libraries_validation/message_string.yml @@ -0,0 +1,7 @@ +# error: post_install_message must be a string (optional) +database: + help: Help text + post_install_message: 1 + files: + - source: "database/database.sh" + target: "%{user_lib_dir}/database.%{user_ext}" diff --git a/spec/fixtures/libraries_validation/root_hash.yml b/spec/fixtures/libraries_validation/root_hash.yml new file mode 100644 index 00000000..374aae0a --- /dev/null +++ b/spec/fixtures/libraries_validation/root_hash.yml @@ -0,0 +1,6 @@ +# error: must be a hash +- database: + help: Add database utilities + files: + - source: "database/database.sh" + target: "%{user_lib_dir}/database.%{user_ext}" diff --git a/spec/fixtures/libraries_validation/usage_string.yml b/spec/fixtures/libraries_validation/usage_string.yml new file mode 100644 index 00000000..8e9207e4 --- /dev/null +++ b/spec/fixtures/libraries_validation/usage_string.yml @@ -0,0 +1,7 @@ +# error: usage must be a string (optional) +database: + help: Help text + usage: 1 + files: + - source: "database/database.sh" + target: "%{user_lib_dir}/database.%{user_ext}"