|
| 1 | +require 'spec_helper' |
| 2 | +require 'open3' |
| 3 | +require 'tmpdir' |
| 4 | +require 'fileutils' |
| 5 | + |
| 6 | +describe 'CI test change classification' do |
| 7 | + around do |example| |
| 8 | + Dir.mktmpdir('test-changes') do |directory| |
| 9 | + @repository = directory |
| 10 | + example.run |
| 11 | + end |
| 12 | + end |
| 13 | + |
| 14 | + before do |
| 15 | + git('init', '--quiet') |
| 16 | + git('config', 'user.name', 'CI') |
| 17 | + git('config', 'user.email', 'ci@example.invalid') |
| 18 | + @base = commit('README.md') |
| 19 | + end |
| 20 | + |
| 21 | + def git(*args) |
| 22 | + output, error, status = Open3.capture3('git', *args, chdir: @repository) |
| 23 | + raise error unless status.success? |
| 24 | + |
| 25 | + output.strip |
| 26 | + end |
| 27 | + |
| 28 | + def commit(path) |
| 29 | + FileUtils.mkdir_p(File.dirname(File.join(@repository, path))) |
| 30 | + File.write(File.join(@repository, path), "content for #{path}\n") |
| 31 | + git('add', '--all') |
| 32 | + git('commit', '--quiet', '-m', 'Test change') |
| 33 | + git('rev-parse', 'HEAD') |
| 34 | + end |
| 35 | + |
| 36 | + def classify(base: @base, head: git('rev-parse', 'HEAD'), event: 'pull_request') |
| 37 | + output = File.join(@repository, 'output') |
| 38 | + script = File.expand_path('../../.github/scripts/test-changes.sh', __dir__) |
| 39 | + _, error, status = Open3.capture3( |
| 40 | + { 'EVENT_NAME' => event, 'BASE_SHA' => base, 'HEAD_SHA' => head, 'GITHUB_OUTPUT' => output }, |
| 41 | + 'bash', script, chdir: @repository |
| 42 | + ) |
| 43 | + raise error unless status.success? |
| 44 | + |
| 45 | + File.read(output).strip |
| 46 | + end |
| 47 | + |
| 48 | + it 'skips Markdown and documentation images' do |
| 49 | + commit('docs/guide.md') |
| 50 | + commit('images/example.png') |
| 51 | + expect(classify).to eq 'run_tests=false' |
| 52 | + end |
| 53 | + |
| 54 | + it 'runs tests when documentation is mixed with runtime changes' do |
| 55 | + commit('UPGRADING.md') |
| 56 | + commit('public/styles.css') |
| 57 | + expect(classify).to eq 'run_tests=true' |
| 58 | + end |
| 59 | + |
| 60 | + it 'does not ignore a code file renamed to Markdown' do |
| 61 | + @base = commit('lib/example.rb') |
| 62 | + File.rename(File.join(@repository, 'lib/example.rb'), File.join(@repository, 'example.md')) |
| 63 | + git('add', '--all') |
| 64 | + git('commit', '--quiet', '-m', 'Rename') |
| 65 | + expect(classify).to eq 'run_tests=true' |
| 66 | + end |
| 67 | + |
| 68 | + it 'compares pull requests from their merge base, not unrelated base-branch changes' do |
| 69 | + head = commit('CHANGELOG.md') |
| 70 | + git('checkout', '--quiet', '--detach', @base) |
| 71 | + base = commit('lib/base-only.rb') |
| 72 | + expect(classify(base: base, head: head)).to eq 'run_tests=false' |
| 73 | + end |
| 74 | + |
| 75 | + it 'includes removed code when a push rewrites history' do |
| 76 | + before = commit('lib/removed.rb') |
| 77 | + git('checkout', '--quiet', '--detach', @base) |
| 78 | + head = commit('CHANGELOG.md') |
| 79 | + expect(classify(base: before, head: head, event: 'push')).to eq 'run_tests=true' |
| 80 | + end |
| 81 | + |
| 82 | + it 'runs the full matrix for a new branch' do |
| 83 | + expect(classify(base: '0' * 40, event: 'push')).to eq 'run_tests=true' |
| 84 | + end |
| 85 | + |
| 86 | + it 'runs the full matrix when a previous pushed revision is unavailable' do |
| 87 | + expect(classify(base: 'f' * 40, event: 'push')).to eq 'run_tests=true' |
| 88 | + end |
| 89 | + |
| 90 | + it 'fails instead of silently skipping tests when a pull request comparison is invalid' do |
| 91 | + expect { classify(base: 'f' * 40) }.to raise_error(RuntimeError) |
| 92 | + end |
| 93 | +end |
0 commit comments