Skip to content

Commit 914e7b6

Browse files
committed
add formatter specs
1 parent 149ce5f commit 914e7b6

File tree

9 files changed

+130
-4
lines changed

9 files changed

+130
-4
lines changed

lib/bashly/exceptions.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ class Interrupt < Interrupt; end
33
class Error < StandardError; end
44
class InitError < Error; end
55
class ConfigurationError < Error; end
6-
class DependencyError < Error; end
76
end

lib/bashly/script/formatter.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Formatter
1010

1111
def initialize(script, mode: nil)
1212
@script = script
13-
@mode = mode
13+
@mode = mode || :internal
1414
end
1515

1616
def formatted_script
@@ -30,8 +30,15 @@ def shfmt_result
3030

3131
def custom_formatter_result(command)
3232
command = Shellwords.split command if command.is_a? String
33-
output, error, status = Open3.capture3 *command, stdin_data: script
34-
raise DependencyError, "Failed running g`#{command}`:\n\n#{error}" unless status.success?
33+
34+
begin
35+
output, error, status = Open3.capture3(*command, stdin_data: script)
36+
rescue Errno::ENOENT => e
37+
raise Error, "Command not found: `#{command.join(' ')}`"
38+
end
39+
40+
raise Error, "Failed running `#{command.join(' ')}`:\n\n#{error}" unless status.success?
41+
3542

3643
output
3744
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#<Bashly::Error:"Failed running `shfmt --diff`:\n\n">
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#<Bashly::Error: Command not found: `my_glorious_formatter`>

spec/approvals/formatter/internal

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
3+
funk() {
4+
cat <<EOF
5+
start multiline heredoc test
6+
7+
end multiline heredoc test
8+
EOF
9+
10+
echo "unnecessary multiline test"
11+
12+
echo "unnecessary multiline test complete"
13+
echo "rogue indentation"
14+
}

spec/approvals/formatter/shfmt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
3+
funk() {
4+
cat <<EOF
5+
start multiline heredoc test
6+
7+
8+
9+
end multiline heredoc test
10+
EOF
11+
12+
echo "unnecessary multiline test"
13+
14+
echo "unnecessary multiline test complete"
15+
echo "rogue indentation"
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
3+
funk()
4+
{
5+
cat <<EOF
6+
start multiline heredoc test
7+
8+
9+
10+
end multiline heredoc test
11+
EOF
12+
13+
echo "unnecessary multiline test"
14+
15+
echo "unnecessary multiline test complete"
16+
echo "rogue indentation"
17+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
describe Script::Formatter do
2+
subject { described_class.new script, mode: mode }
3+
4+
let(:script) { File.read "spec/fixtures/formatter/#{script_id}.sh" }
5+
let(:script_id) { :simple }
6+
let(:mode) { nil }
7+
8+
describe '#formatted_script' do
9+
it 'formats the script using the internal formatter' do
10+
expect(subject.formatted_script).to match_approval 'formatter/internal'
11+
end
12+
13+
context 'with mode: :none' do
14+
let(:mode) { :none }
15+
16+
it 'returns the original script' do
17+
expect(subject.formatted_script).to eq script
18+
end
19+
end
20+
21+
context 'with mode: :shfmt' do
22+
let(:mode) { :shfmt }
23+
24+
it 'uses shfmt to format the script' do
25+
expect(subject.formatted_script).to match_approval 'formatter/shfmt'
26+
end
27+
end
28+
29+
context 'with mode: shfmt (string)' do
30+
let(:mode) { 'shfmt --func-next-line' }
31+
32+
it 'uses the given command shfmt to format the script' do
33+
expect(subject.formatted_script).to match_approval 'formatter/shfmt-custom'
34+
end
35+
end
36+
37+
context 'when the external command does not exist' do
38+
let(:mode) { 'my_glorious_formatter' }
39+
40+
it 'raises a Bashly::Error' do
41+
expect { subject.formatted_script }.to raise_approval 'formatter/error-not-found'
42+
end
43+
end
44+
45+
context 'when the external command fails' do
46+
let(:mode) { 'shfmt --diff' }
47+
48+
it 'raises a a Bashly::Error' do
49+
expect { subject.formatted_script }.to raise_approval 'formatter/error-failure'
50+
end
51+
end
52+
end
53+
end

spec/fixtures/formatter/simple.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
3+
funk() {
4+
cat <<EOF
5+
start multiline heredoc test
6+
7+
8+
9+
end multiline heredoc test
10+
EOF
11+
12+
echo "unnecessary multiline test"
13+
14+
15+
16+
echo "unnecessary multiline test complete"
17+
echo "rogue indentation"
18+
}

0 commit comments

Comments
 (0)