Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/puppet-lint/plugins/check_strict_indent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ def check
indent -= 1
colon_indent = nil
end
elsif next_token.type == :SEMIC and !colon_indent.nil? and
%i[INDENT NEWLINE].include?(next_token.prev_token.type) and
(next_token.next_token.nil? or next_token.next_token.type == :NEWLINE)
# For a lone semicolon within a block decrement immediately. Use temp_indent because
# indent will be decremented in the next line by the prev_token logic above.
temp_indent -= 1
end
next_token = next_token.next_token
end
Expand Down
15 changes: 15 additions & 0 deletions spec/fixtures/pass/semicolon.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
file {
default:
ensure => file,
owner => 'root',
group => 'wheel',
mode => '0600',
;
['ssh_host_dsa_key', 'ssh_host_key', 'ssh_host_rsa_key']:
# use all defaults
;
['ssh_config', 'ssh_host_dsa_key.pub', 'ssh_host_key.pub', 'ssh_host_rsa_key.pub', 'sshd_config']:
# override mode
mode => '0644',
;
}
73 changes: 73 additions & 0 deletions spec/puppet-lint/plugins/check_strict_indent_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
require 'spec_helper'
# rubocop:disable Lint/RedundantRequireStatement
require 'pp'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# rubocop:enable Lint/RedundantRequireStatement

describe 'strict_indent' do
before do
Expand Down Expand Up @@ -90,4 +93,74 @@ class () {}
expect(problems).to have(0).problems
end
end

context 'misplaced resource semicolons' do
before do
PuppetLint.configuration.fix = true
end

after do
PuppetLint.configuration.fix = false
end

let(:code) do
<<~EOF
file {
default:
ensure => file,
owner => 'root',
group => 'wheel',
mode => '0600',
;
['ssh_host_dsa_key', 'ssh_host_key', 'ssh_host_rsa_key']:
# use all defaults
;
['ssh_config', 'ssh_host_dsa_key.pub', 'ssh_host_key.pub', 'ssh_host_rsa_key.pub', 'sshd_config']:
# override mode
mode => '0644',
;
}
EOF
end

let(:fixed) do
<<~EOF
file {
default:
ensure => file,
owner => 'root',
group => 'wheel',
mode => '0600',
;
['ssh_host_dsa_key', 'ssh_host_key', 'ssh_host_rsa_key']:
# use all defaults
;
['ssh_config', 'ssh_host_dsa_key.pub', 'ssh_host_key.pub', 'ssh_host_rsa_key.pub', 'sshd_config']:
# override mode
mode => '0644',
;
}
EOF
end

it 'detects three problems' do
expect(problems).to have(3).problem
end

it 'fixes the first problem' do
expect(problems).to contain_fixed('indent should be 2 chars and is 4').on_line(7).in_column(1)
end

it 'fixes the second problem' do
expect(problems).to contain_fixed('indent should be 2 chars and is 4').on_line(10).in_column(1)
end

it 'fixes the third problem' do
expect(problems).to contain_fixed('indent should be 2 chars and is 0').on_line(14).in_column(1)
end

it 'moves the semicolons' do
expect(manifest).to eq fixed
end
end
end