|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +describe Overcommit::Hook::PreCommit::OxFmt do |
| 6 | + let(:config) { Overcommit::ConfigurationLoader.default_configuration } |
| 7 | + let(:context) { double('context') } |
| 8 | + subject { described_class.new(config, context) } |
| 9 | + |
| 10 | + before do |
| 11 | + subject.stub(:applicable_files).and_return(%w[file1.js file2.js]) |
| 12 | + end |
| 13 | + |
| 14 | + context 'when oxfmt is unable to run' do |
| 15 | + let(:result) { double('result') } |
| 16 | + |
| 17 | + before do |
| 18 | + result.stub(:stderr).and_return('SyntaxError: Use of const in strict mode.') |
| 19 | + result.stub(:stdout).and_return('') |
| 20 | + |
| 21 | + result.stub(:success?).and_return(false) |
| 22 | + subject.stub(:execute).and_return(result) |
| 23 | + end |
| 24 | + |
| 25 | + it { should fail_hook } |
| 26 | + end |
| 27 | + |
| 28 | + context 'when oxfmt exits successfully' do |
| 29 | + let(:result) { double('result') } |
| 30 | + |
| 31 | + before do |
| 32 | + result.stub(:success?).and_return(true) |
| 33 | + subject.stub(:execute).and_return(result) |
| 34 | + end |
| 35 | + |
| 36 | + context 'with no output' do |
| 37 | + before do |
| 38 | + result.stub(:stdout).and_return('') |
| 39 | + end |
| 40 | + |
| 41 | + it { should pass } |
| 42 | + end |
| 43 | + |
| 44 | + context 'and it reports an error' do |
| 45 | + before do |
| 46 | + result.stub(:stdout).and_return([ |
| 47 | + 'Checking formatting...', |
| 48 | + '', |
| 49 | + 'README.md (66ms)', |
| 50 | + '', |
| 51 | + 'Format issues found in above 1 files. Run without `--check` to fix.', |
| 52 | + 'Finished in 66ms on 1 files using 8 threads.' |
| 53 | + ].join("\n")) |
| 54 | + end |
| 55 | + |
| 56 | + it { should fail_hook } |
| 57 | + end |
| 58 | + |
| 59 | + context 'and it doesnt count false positives error messages' do |
| 60 | + before do |
| 61 | + result.stub(:stdout).and_return([ |
| 62 | + '$ yarn oxfmt --check /app/project/Error.ts', |
| 63 | + '$ /app/project/node_modules/.bin/oxfmt --check /app/project/Error.ts', |
| 64 | + '', |
| 65 | + ].join("\n")) |
| 66 | + end |
| 67 | + |
| 68 | + it { should pass } |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + context 'when oxfmt exits unsucessfully' do |
| 73 | + let(:result) { double('result') } |
| 74 | + |
| 75 | + before do |
| 76 | + result.stub(:success?).and_return(false) |
| 77 | + subject.stub(:execute).and_return(result) |
| 78 | + end |
| 79 | + |
| 80 | + context 'and it reports an error' do |
| 81 | + before do |
| 82 | + result.stub(:stdout).and_return([ |
| 83 | + 'Checking formatting...', |
| 84 | + '', |
| 85 | + 'README.md (66ms)', |
| 86 | + '', |
| 87 | + 'Format issues found in above 1 files. Run without `--check` to fix.', |
| 88 | + 'Finished in 66ms on 1 files using 8 threads.' |
| 89 | + ].join("\n")) |
| 90 | + end |
| 91 | + |
| 92 | + it { should fail_hook } |
| 93 | + end |
| 94 | + end |
| 95 | +end |
0 commit comments