Skip to content

Commit

Permalink
Test for "tags" being anything other than an array of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
domcleal committed Nov 29, 2016
1 parent 3b63cb0 commit ad7fddc
Show file tree
Hide file tree
Showing 10 changed files with 321 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ Metrics/LineLength:
Metrics/MethodLength:
Enabled: false

Metrics/ModuleLength:
Enabled: false

Metrics/PerceivedComplexity:
Enabled: false
16 changes: 16 additions & 0 deletions lib/metadata_json_lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ def parse(metadata)
error_state = true if options[:strict_license]
end

error_state = tag_errors?(parsed['tags']) unless parsed['tags'].nil?

return unless error_state
if options[:fail_on_warnings] == true
abort("Errors found in #{metadata}")
Expand Down Expand Up @@ -135,4 +137,18 @@ def open_ended?(module_end)
SemanticPuppet::VersionRange.parse(module_end).end == SemanticPuppet::Version::MAX
end
module_function :open_ended?

def tag_errors?(tags)
# Tags must be an array of strings
if !tags.is_a?(Array) || tags.any? { |tag| !tag.is_a?(String) }
puts 'Error: tags must be an array of strings'
true
elsif tags.any? { |tag| tag =~ /\s/ }
puts 'Error: tags must not contain any whitespace'
true
else
false
end
end
module_function :tag_errors?
end
1 change: 1 addition & 0 deletions tests/perfect/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"source": "git://github.com/puppetlabs/puppet-postgresql.git",
"project_page": "https://github.com/puppetlabs/puppet-postgresql",
"issues_url": "https://github.com/puppetlabs/puppet-postgresql/issues",
"tags": ["things", "stuff"],
"operatingsystem_support": [
{
"operatingsystem": "RedHat",
Expand Down
2 changes: 2 additions & 0 deletions tests/tags_not_array/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$LOAD_PATH.unshift(File.expand_path('../../../lib', __FILE__))
require 'metadata-json-lint/rake_task'
96 changes: 96 additions & 0 deletions tests/tags_not_array/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
"name": "puppetlabs-postgresql",
"version": "3.4.1",
"author": "Inkling/Puppet Labs",
"summary": "PostgreSQL defined resource types",
"license": "Apache-2.0",
"source": "git://github.com/puppetlabs/puppet-postgresql.git",
"project_page": "https://github.com/puppetlabs/puppet-postgresql",
"issues_url": "https://github.com/puppetlabs/puppet-postgresql/issues",
"tags": "broken",
"operatingsystem_support": [
{
"operatingsystem": "RedHat",
"operatingsystemrelease": [
"5",
"6",
"7"
]
},
{
"operatingsystem": "CentOS",
"operatingsystemrelease": [
"5",
"6",
"7"
]
},
{
"operatingsystem": "OracleLinux",
"operatingsystemrelease": [
"5",
"6",
"7"
]
},
{
"operatingsystem": "Scientific",
"operatingsystemrelease": [
"5",
"6",
"7"
]
},
{
"operatingsystem": "Debian",
"operatingsystemrelease": [
"6",
"7"
]
},
{
"operatingsystem": "Ubuntu",
"operatingsystemrelease": [
"10.04",
"12.04",
"14.04"
]
}
],
"requirements": [
{
"name": "pe",
"version_requirement": ">= 3.2.0 < 3.4.0"
},
{
"name": "puppet",
"version_requirement": "3.x"
}
],
"dependencies": [
{
"name": "puppetlabs/stdlib",
"version_requirement": "1.2.3"
},
{
"name": "puppetlabs/apt",
"version_requirement": "< 1.2.3"
},
{
"name": "puppetlabs/puppetdb",
"version_requirement": "<= 1.2.3"
},
{
"name": "puppetlabs/vcsrepo",
"version_requirement": ">= 1.0.0 < 2.0.0"
},
{
"name": "puppetlabs/rabbitmq",
"version_requirement": "1.x"
},
{
"name": "puppetlabs/motd",
"version_requirement": "1.2.x"
}
]
}
2 changes: 2 additions & 0 deletions tests/tags_not_strings/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$LOAD_PATH.unshift(File.expand_path('../../../lib', __FILE__))
require 'metadata-json-lint/rake_task'
96 changes: 96 additions & 0 deletions tests/tags_not_strings/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
"name": "puppetlabs-postgresql",
"version": "3.4.1",
"author": "Inkling/Puppet Labs",
"summary": "PostgreSQL defined resource types",
"license": "Apache-2.0",
"source": "git://github.com/puppetlabs/puppet-postgresql.git",
"project_page": "https://github.com/puppetlabs/puppet-postgresql",
"issues_url": "https://github.com/puppetlabs/puppet-postgresql/issues",
"tags": [42],
"operatingsystem_support": [
{
"operatingsystem": "RedHat",
"operatingsystemrelease": [
"5",
"6",
"7"
]
},
{
"operatingsystem": "CentOS",
"operatingsystemrelease": [
"5",
"6",
"7"
]
},
{
"operatingsystem": "OracleLinux",
"operatingsystemrelease": [
"5",
"6",
"7"
]
},
{
"operatingsystem": "Scientific",
"operatingsystemrelease": [
"5",
"6",
"7"
]
},
{
"operatingsystem": "Debian",
"operatingsystemrelease": [
"6",
"7"
]
},
{
"operatingsystem": "Ubuntu",
"operatingsystemrelease": [
"10.04",
"12.04",
"14.04"
]
}
],
"requirements": [
{
"name": "pe",
"version_requirement": ">= 3.2.0 < 3.4.0"
},
{
"name": "puppet",
"version_requirement": "3.x"
}
],
"dependencies": [
{
"name": "puppetlabs/stdlib",
"version_requirement": "1.2.3"
},
{
"name": "puppetlabs/apt",
"version_requirement": "< 1.2.3"
},
{
"name": "puppetlabs/puppetdb",
"version_requirement": "<= 1.2.3"
},
{
"name": "puppetlabs/vcsrepo",
"version_requirement": ">= 1.0.0 < 2.0.0"
},
{
"name": "puppetlabs/rabbitmq",
"version_requirement": "1.x"
},
{
"name": "puppetlabs/motd",
"version_requirement": "1.2.x"
}
]
}
2 changes: 2 additions & 0 deletions tests/tags_whitespace/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$LOAD_PATH.unshift(File.expand_path('../../../lib', __FILE__))
require 'metadata-json-lint/rake_task'
96 changes: 96 additions & 0 deletions tests/tags_whitespace/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
"name": "puppetlabs-postgresql",
"version": "3.4.1",
"author": "Inkling/Puppet Labs",
"summary": "PostgreSQL defined resource types",
"license": "Apache-2.0",
"source": "git://github.com/puppetlabs/puppet-postgresql.git",
"project_page": "https://github.com/puppetlabs/puppet-postgresql",
"issues_url": "https://github.com/puppetlabs/puppet-postgresql/issues",
"tags": ["contains whitespace"],
"operatingsystem_support": [
{
"operatingsystem": "RedHat",
"operatingsystemrelease": [
"5",
"6",
"7"
]
},
{
"operatingsystem": "CentOS",
"operatingsystemrelease": [
"5",
"6",
"7"
]
},
{
"operatingsystem": "OracleLinux",
"operatingsystemrelease": [
"5",
"6",
"7"
]
},
{
"operatingsystem": "Scientific",
"operatingsystemrelease": [
"5",
"6",
"7"
]
},
{
"operatingsystem": "Debian",
"operatingsystemrelease": [
"6",
"7"
]
},
{
"operatingsystem": "Ubuntu",
"operatingsystemrelease": [
"10.04",
"12.04",
"14.04"
]
}
],
"requirements": [
{
"name": "pe",
"version_requirement": ">= 3.2.0 < 3.4.0"
},
{
"name": "puppet",
"version_requirement": "3.x"
}
],
"dependencies": [
{
"name": "puppetlabs/stdlib",
"version_requirement": "1.2.3"
},
{
"name": "puppetlabs/apt",
"version_requirement": "< 1.2.3"
},
{
"name": "puppetlabs/puppetdb",
"version_requirement": "<= 1.2.3"
},
{
"name": "puppetlabs/vcsrepo",
"version_requirement": ">= 1.0.0 < 2.0.0"
},
{
"name": "puppetlabs/rabbitmq",
"version_requirement": "1.x"
},
{
"name": "puppetlabs/motd",
"version_requirement": "1.2.x"
}
]
}
7 changes: 7 additions & 0 deletions tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ test "proprietary" $SUCCESS
# Run without a metadata.json or Rakefile, expect FAILURE
test "no_files" $FAILURE

# Run one with "tags" set to a string instead of an array, expect FAILURE
test "tags_not_array" $FAILURE
# and non-string values inside an array
test "tags_not_strings" $FAILURE
# and whitespace inside strings
test "tags_whitespace" $FAILURE

# Test running without specifying file to parse
cd perfect
bundle exec metadata-json-lint
Expand Down

0 comments on commit ad7fddc

Please sign in to comment.