|
| 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 |
0 commit comments