|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | +require 'overcommit/hook_context/diff' |
| 5 | + |
| 6 | +describe Overcommit::HookContext::Diff do |
| 7 | + let(:config) { double('config') } |
| 8 | + let(:args) { [] } |
| 9 | + let(:input) { double('input') } |
| 10 | + let(:context) { described_class.new(config, args, input, diff: 'master') } |
| 11 | + |
| 12 | + describe '#modified_files' do |
| 13 | + subject { context.modified_files } |
| 14 | + |
| 15 | + context 'when repo contains no files' do |
| 16 | + around do |example| |
| 17 | + repo do |
| 18 | + `git commit --allow-empty -m "Initial commit"` |
| 19 | + `git checkout -b other-branch 2>&1` |
| 20 | + example.run |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + it { should be_empty } |
| 25 | + end |
| 26 | + |
| 27 | + context 'when the repo contains files that are unchanged from the ref' do |
| 28 | + around do |example| |
| 29 | + repo do |
| 30 | + touch('some-file') |
| 31 | + `git add some-file` |
| 32 | + touch('some-other-file') |
| 33 | + `git add some-other-file` |
| 34 | + `git commit -m "Add files"` |
| 35 | + `git checkout -b other-branch 2>&1` |
| 36 | + example.run |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + it { should be_empty } |
| 41 | + end |
| 42 | + |
| 43 | + context 'when repo contains files that have been changed from the ref' do |
| 44 | + around do |example| |
| 45 | + repo do |
| 46 | + touch('some-file') |
| 47 | + `git add some-file` |
| 48 | + touch('some-other-file') |
| 49 | + `git add some-other-file` |
| 50 | + `git commit -m "Add files"` |
| 51 | + `git checkout -b other-branch 2>&1` |
| 52 | + File.open('some-file', 'w') { |f| f.write("hello\n") } |
| 53 | + `git add some-file` |
| 54 | + `git commit -m "Edit file"` |
| 55 | + example.run |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + it { should == %w[some-file].map { |file| File.expand_path(file) } } |
| 60 | + end |
| 61 | + |
| 62 | + context 'when repo contains submodules' do |
| 63 | + around do |example| |
| 64 | + submodule = repo do |
| 65 | + touch 'foo' |
| 66 | + `git add foo` |
| 67 | + `git commit -m "Initial commit"` |
| 68 | + end |
| 69 | + |
| 70 | + repo do |
| 71 | + `git submodule add #{submodule} test-sub 2>&1 > #{File::NULL}` |
| 72 | + `git commit --allow-empty -m "Initial commit"` |
| 73 | + `git checkout -b other-branch 2>&1` |
| 74 | + example.run |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + it { should_not include File.expand_path('test-sub') } |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + describe '#modified_lines_in_file' do |
| 83 | + let(:modified_file) { 'some-file' } |
| 84 | + subject { context.modified_lines_in_file(modified_file) } |
| 85 | + |
| 86 | + context 'when file contains a trailing newline' do |
| 87 | + around do |example| |
| 88 | + repo do |
| 89 | + touch(modified_file) |
| 90 | + `git add #{modified_file}` |
| 91 | + `git commit -m "Add file"` |
| 92 | + `git checkout -b other-branch 2>&1` |
| 93 | + File.open(modified_file, 'w') { |f| (1..3).each { |i| f.write("#{i}\n") } } |
| 94 | + `git add #{modified_file}` |
| 95 | + `git commit -m "Edit file"` |
| 96 | + example.run |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + it { should == Set.new(1..3) } |
| 101 | + end |
| 102 | + |
| 103 | + context 'when file does not contain a trailing newline' do |
| 104 | + around do |example| |
| 105 | + repo do |
| 106 | + touch(modified_file) |
| 107 | + `git add #{modified_file}` |
| 108 | + `git commit -m "Add file"` |
| 109 | + `git checkout -b other-branch 2>&1` |
| 110 | + File.open(modified_file, 'w') do |f| |
| 111 | + (1..2).each { |i| f.write("#{i}\n") } |
| 112 | + f.write(3) |
| 113 | + end |
| 114 | + `git add #{modified_file}` |
| 115 | + `git commit -m "Edit file"` |
| 116 | + example.run |
| 117 | + end |
| 118 | + end |
| 119 | + |
| 120 | + it { should == Set.new(1..3) } |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | + describe '#hook_type_name' do |
| 125 | + subject { context.hook_type_name } |
| 126 | + |
| 127 | + it { should == 'pre_commit' } |
| 128 | + end |
| 129 | + |
| 130 | + describe '#hook_script_name' do |
| 131 | + subject { context.hook_script_name } |
| 132 | + |
| 133 | + it { should == 'pre-commit' } |
| 134 | + end |
| 135 | + |
| 136 | + describe '#initial_commit?' do |
| 137 | + subject { context.initial_commit? } |
| 138 | + |
| 139 | + before { Overcommit::GitRepo.stub(:initial_commit?).and_return(true) } |
| 140 | + |
| 141 | + it { should == true } |
| 142 | + end |
| 143 | +end |
0 commit comments