Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #552 from binford2k/dLobatog-code_outside_class_block
Updates PR for #223
  • Loading branch information
rnelson0 committed Oct 5, 2016
2 parents 37961f0 + 2c65820 commit 32e93d4
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
18 changes: 18 additions & 0 deletions lib/puppet-lint/plugins/check_classes.rb
Expand Up @@ -170,6 +170,24 @@ def check
end
end

# Public: Test that no code is outside of a class or define scope.
PuppetLint.new_check(:code_on_top_scope) do
def check
class_scope = (class_indexes + defined_type_indexes).map { |e| tokens[e[:start]..e[:end]] }.flatten
top_scope = tokens - class_scope

top_scope.each do |token|
unless formatting_tokens.include? token.type
notify :warning, {
:message => "code outside of class or define block - #{token.value}",
:line => token.line,
:column => token.column
}
end
end
end
end

# Public: Test the manifest tokens for any variables that are referenced in
# the manifest. If the variables are not fully qualified or one of the
# variables automatically created in the scope, check that they have been
Expand Down
5 changes: 4 additions & 1 deletion spec/fixtures/test/manifests/ignore_reason.pp
@@ -1 +1,4 @@
"test" # lint:ignore:double_quoted_strings for a good reason
# test::ignore_reason
class test::ignore_reason {
$foo = "test" # lint:ignore:double_quoted_strings for a good reason
}
2 changes: 1 addition & 1 deletion spec/puppet-lint/bin_spec.rb
Expand Up @@ -291,7 +291,7 @@ def initialize(args)

its(:exitstatus) { is_expected.to eq(0) }
its(:stdout) { is_expected.to eq([
"IGNORED: double quoted string containing no variables on line 1",
"IGNORED: double quoted string containing no variables on line 3",
" for a good reason",
].join("\n")) }
end
Expand Down
42 changes: 42 additions & 0 deletions spec/puppet-lint/plugins/check_classes/code_on_top_scope_spec.rb
@@ -0,0 +1,42 @@
require 'spec_helper'

describe 'code_on_top_scope' do
describe 'comments outside class block' do
let(:code) { "
# Baz
class foo:bar {
}"
}

its(:problems) { should be_empty }
end

describe 'new lines outside of class-define block' do
let(:code) { "
class foo:bar {
}
"
}

its(:problems) { should be_empty }
end
describe 'code outside class block' do
let(:code) { "
include('something')
# Baz
class foo:bar {
}
define whatever {
}"
}

its(:problems) {
should contain_warning("code outside of class or define block - include")
should have(4).problems
}
end
end

0 comments on commit 32e93d4

Please sign in to comment.