|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | +require 'open3' |
| 5 | +require 'tmpdir' |
| 6 | + |
| 7 | +describe 'pathspec-rb CLI' do |
| 8 | + let(:cli_path) { File.expand_path('../../bin/pathspec-rb', __dir__) } |
| 9 | + let(:lib_path) { File.expand_path('../../lib', __dir__) } |
| 10 | + let(:gitignore_simple) { File.expand_path('../files/gitignore_simple', __dir__) } |
| 11 | + let(:gitignore_readme) { File.expand_path('../files/gitignore_readme', __dir__) } |
| 12 | + let(:regex_simple) { File.expand_path('../files/regex_simple', __dir__) } |
| 13 | + |
| 14 | + def run_cli(*args) |
| 15 | + env = { 'RUBYLIB' => lib_path } |
| 16 | + stdout, stderr, status = Open3.capture3(env, 'ruby', cli_path, *args) |
| 17 | + [stdout, stderr, status] |
| 18 | + end |
| 19 | + |
| 20 | + describe 'help and errors' do |
| 21 | + it 'shows help when no arguments provided' do |
| 22 | + stdout, _stderr, status = run_cli |
| 23 | + expect(stdout).to include('Usage: pathspec-rb') |
| 24 | + expect(stdout).to include('Subcommands:') |
| 25 | + expect(stdout).to include('specs_match') |
| 26 | + expect(stdout).to include('tree') |
| 27 | + expect(stdout).to include('match') |
| 28 | + expect(status.exitstatus).to eq(2) |
| 29 | + end |
| 30 | + |
| 31 | + it 'shows error for unreadable file' do |
| 32 | + stdout, _stderr, status = run_cli('-f', '/nonexistent/file', 'match', 'test.txt') |
| 33 | + expect(stdout).to include("Error: I couldn't read /nonexistent/file") |
| 34 | + expect(status.exitstatus).to eq(2) |
| 35 | + end |
| 36 | + |
| 37 | + it 'shows error for unknown subcommand' do |
| 38 | + stdout, _stderr, status = run_cli('-f', gitignore_simple, 'unknown_command', 'test.txt') |
| 39 | + expect(stdout).to include('Unknown sub-command unknown_command') |
| 40 | + expect(stdout).to include('Usage: pathspec-rb') |
| 41 | + expect(status.exitstatus).to eq(2) |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + describe 'match subcommand' do |
| 46 | + context 'with matching path' do |
| 47 | + it 'exits with 0' do |
| 48 | + _stdout, _stderr, status = run_cli('-f', gitignore_simple, 'match', 'test.md') |
| 49 | + expect(status.exitstatus).to eq(0) |
| 50 | + end |
| 51 | + |
| 52 | + it 'shows match message with verbose flag' do |
| 53 | + stdout, _stderr, status = run_cli('-f', gitignore_simple, '-v', 'match', 'test.md') |
| 54 | + expect(stdout).to include('test.md matches a spec') |
| 55 | + expect(status.exitstatus).to eq(0) |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + context 'with non-matching path' do |
| 60 | + it 'exits with 1' do |
| 61 | + _stdout, _stderr, status = run_cli('-f', gitignore_simple, 'match', 'other.txt') |
| 62 | + expect(status.exitstatus).to eq(1) |
| 63 | + end |
| 64 | + |
| 65 | + it 'shows no match message with verbose flag' do |
| 66 | + stdout, _stderr, status = run_cli('-f', gitignore_simple, '-v', 'match', 'other.txt') |
| 67 | + expect(stdout).to include('other.txt does not match') |
| 68 | + expect(status.exitstatus).to eq(1) |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + context 'with negated pattern' do |
| 73 | + it 'does not match negated paths' do |
| 74 | + _stdout, _stderr, status = run_cli('-f', gitignore_readme, 'match', 'abc/important.txt') |
| 75 | + expect(status.exitstatus).to eq(1) |
| 76 | + end |
| 77 | + |
| 78 | + it 'matches non-negated paths' do |
| 79 | + _stdout, _stderr, status = run_cli('-f', gitignore_readme, 'match', 'abc/other.txt') |
| 80 | + expect(status.exitstatus).to eq(0) |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + describe 'specs_match subcommand' do |
| 86 | + context 'with matching path' do |
| 87 | + it 'exits with 0 and shows matching specs' do |
| 88 | + stdout, _stderr, status = run_cli('-f', gitignore_readme, 'specs_match', 'abc/def.rb') |
| 89 | + expect(stdout).to include('abc/**') |
| 90 | + expect(status.exitstatus).to eq(0) |
| 91 | + end |
| 92 | + |
| 93 | + it 'shows verbose message with -v flag' do |
| 94 | + stdout, _stderr, status = run_cli('-f', gitignore_readme, '-v', 'specs_match', 'abc/def.rb') |
| 95 | + expect(stdout).to include('abc/def.rb matches the following specs') |
| 96 | + expect(stdout).to include('abc/**') |
| 97 | + expect(status.exitstatus).to eq(0) |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + context 'with non-matching path' do |
| 102 | + it 'exits with 1' do |
| 103 | + _stdout, _stderr, status = run_cli('-f', gitignore_readme, 'specs_match', 'xyz/file.txt') |
| 104 | + expect(status.exitstatus).to eq(1) |
| 105 | + end |
| 106 | + |
| 107 | + it 'shows no match message with verbose flag' do |
| 108 | + stdout, _stderr, status = run_cli('-f', gitignore_readme, '-v', 'specs_match', 'xyz/file.txt') |
| 109 | + expect(stdout).to include('xyz/file.txt does not match any specs') |
| 110 | + expect(status.exitstatus).to eq(1) |
| 111 | + end |
| 112 | + end |
| 113 | + end |
| 114 | + |
| 115 | + describe 'tree subcommand' do |
| 116 | + around do |example| |
| 117 | + Dir.mktmpdir do |temp_dir| |
| 118 | + @temp_dir = temp_dir |
| 119 | + example.run |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + before do |
| 124 | + # Create test directory structure |
| 125 | + FileUtils.mkdir_p(File.join(@temp_dir, 'foo')) |
| 126 | + FileUtils.mkdir_p(File.join(@temp_dir, 'other')) |
| 127 | + FileUtils.touch(File.join(@temp_dir, 'foo', 'test.txt')) |
| 128 | + FileUtils.touch(File.join(@temp_dir, 'foo', 'another.txt')) |
| 129 | + FileUtils.touch(File.join(@temp_dir, 'other', 'file.txt')) |
| 130 | + |
| 131 | + # Create a gitignore that matches foo/** |
| 132 | + @temp_gitignore = File.join(@temp_dir, '.gitignore') |
| 133 | + File.write(@temp_gitignore, "foo/**\n") |
| 134 | + end |
| 135 | + |
| 136 | + context 'with matching files' do |
| 137 | + it 'exits with 0 and lists matching files' do |
| 138 | + stdout, _stderr, status = run_cli('-f', @temp_gitignore, 'tree', @temp_dir) |
| 139 | + expect(stdout).to include('foo') |
| 140 | + expect(stdout.lines.any? { |line| line.include?('other') && !line.include?('another') }).to be false |
| 141 | + expect(status.exitstatus).to eq(0) |
| 142 | + end |
| 143 | + |
| 144 | + it 'shows verbose message with -v flag' do |
| 145 | + stdout, _stderr, status = run_cli('-f', @temp_gitignore, '-v', 'tree', @temp_dir) |
| 146 | + expect(stdout).to include("Files in #{@temp_dir} that match") |
| 147 | + expect(status.exitstatus).to eq(0) |
| 148 | + end |
| 149 | + end |
| 150 | + |
| 151 | + context 'with no matching files' do |
| 152 | + before do |
| 153 | + # Create gitignore with pattern that won't match anything |
| 154 | + File.write(@temp_gitignore, "nomatch/**\n") |
| 155 | + end |
| 156 | + |
| 157 | + it 'exits with 1' do |
| 158 | + _stdout, _stderr, status = run_cli('-f', @temp_gitignore, 'tree', @temp_dir) |
| 159 | + expect(status.exitstatus).to eq(1) |
| 160 | + end |
| 161 | + |
| 162 | + it 'shows no match message with verbose flag' do |
| 163 | + stdout, _stderr, status = run_cli('-f', @temp_gitignore, '-v', 'tree', @temp_dir) |
| 164 | + expect(stdout).to include('No file') |
| 165 | + expect(stdout).to include('matched') |
| 166 | + expect(status.exitstatus).to eq(1) |
| 167 | + end |
| 168 | + end |
| 169 | + end |
| 170 | + |
| 171 | + describe 'type flag' do |
| 172 | + context 'with git type (default)' do |
| 173 | + it 'parses gitignore patterns' do |
| 174 | + _stdout, _stderr, status = run_cli('-f', gitignore_simple, '-t', 'git', 'match', 'test.md') |
| 175 | + expect(status.exitstatus).to eq(0) |
| 176 | + end |
| 177 | + end |
| 178 | + |
| 179 | + context 'with regex type' do |
| 180 | + it 'parses regex patterns' do |
| 181 | + _stdout, _stderr, status = run_cli('-f', regex_simple, '-t', 'regex', 'match', 'foo.md') |
| 182 | + expect(status.exitstatus).to eq(0) |
| 183 | + end |
| 184 | + end |
| 185 | + end |
| 186 | + |
| 187 | + describe 'file flag' do |
| 188 | + it 'uses default .gitignore when not specified' do |
| 189 | + Dir.mktmpdir do |dir| |
| 190 | + gitignore_path = File.join(dir, '.gitignore') |
| 191 | + File.write(gitignore_path, "test/**\n") |
| 192 | + |
| 193 | + Dir.chdir(dir) do |
| 194 | + _stdout, _stderr, status = run_cli('match', 'test/file.txt') |
| 195 | + expect(status.exitstatus).to eq(0) |
| 196 | + end |
| 197 | + end |
| 198 | + end |
| 199 | + |
| 200 | + it 'uses specified file with -f flag' do |
| 201 | + _stdout, _stderr, status = run_cli('-f', gitignore_simple, 'match', 'test.md') |
| 202 | + expect(status.exitstatus).to eq(0) |
| 203 | + end |
| 204 | + |
| 205 | + it 'uses specified file with --file flag' do |
| 206 | + _stdout, _stderr, status = run_cli('--file', gitignore_simple, 'match', 'test.md') |
| 207 | + expect(status.exitstatus).to eq(0) |
| 208 | + end |
| 209 | + end |
| 210 | + |
| 211 | + describe 'empty string match command' do |
| 212 | + it 'treats empty string as match command' do |
| 213 | + _stdout, _stderr, status = run_cli('-f', gitignore_simple, '', 'test.md') |
| 214 | + expect(status.exitstatus).to eq(0) |
| 215 | + end |
| 216 | + end |
| 217 | +end |
0 commit comments