|
| 1 | +require_relative "./helper" |
| 2 | +require 'tmpdir' |
| 3 | +require 'fileutils' |
| 4 | +require 'open3' |
| 5 | + |
| 6 | +class TestCLIIntegration < Minitest::Test |
| 7 | + def setup |
| 8 | + @temp_dir = Dir.mktmpdir('linguist_cli_test') |
| 9 | + @original_dir = Dir.pwd |
| 10 | + Dir.chdir(@temp_dir) |
| 11 | + |
| 12 | + # Initialize a git repository |
| 13 | + system("git init --quiet") |
| 14 | + system("git config user.name 'Test User'") |
| 15 | + system("git config user.email 'test@example.com'") |
| 16 | + end |
| 17 | + |
| 18 | + def teardown |
| 19 | + Dir.chdir(@original_dir) |
| 20 | + FileUtils.rm_rf(@temp_dir) |
| 21 | + end |
| 22 | + |
| 23 | + def test_strategies_flag_with_gitattributes_override |
| 24 | + # Create a .gitattributes file that overrides language detection |
| 25 | + File.write('.gitattributes', "*.special linguist-language=Ruby\n") |
| 26 | + |
| 27 | + # Create a test file with a non-Ruby extension but Ruby content |
| 28 | + File.write('test.special', "puts 'Hello, World!'\n") |
| 29 | + |
| 30 | + # Stage and commit the files |
| 31 | + system("git add .") |
| 32 | + system("git commit -m 'Initial commit' --quiet") |
| 33 | + |
| 34 | + # Run github-linguist with --strategies flag from the original directory but pointing to our test file |
| 35 | + stdout, stderr, status = Open3.capture3( |
| 36 | + "bundle", "exec", "github-linguist", File.join(@temp_dir, "test.special"), "--strategies", |
| 37 | + chdir: @original_dir |
| 38 | + ) |
| 39 | + |
| 40 | + assert status.success?, "CLI command failed: #{stderr}" |
| 41 | + assert_match(/language:\s+Ruby/, stdout, "Should detect Ruby language") |
| 42 | + assert_match(/strategy:\s+.*\(overridden by \.gitattributes\)/, stdout, "Should show override in strategy") |
| 43 | + end |
| 44 | + |
| 45 | + def test_strategies_flag_with_normal_detection |
| 46 | + # Create a normal Ruby file |
| 47 | + File.write('test.rb', "puts 'Hello, World!'\n") |
| 48 | + |
| 49 | + # Stage and commit the file |
| 50 | + system("git add .") |
| 51 | + system("git commit -m 'Initial commit' --quiet") |
| 52 | + |
| 53 | + # Run github-linguist with --strategies flag |
| 54 | + stdout, stderr, status = Open3.capture3( |
| 55 | + "bundle", "exec", "github-linguist", File.join(@temp_dir, "test.rb"), "--strategies", |
| 56 | + chdir: @original_dir |
| 57 | + ) |
| 58 | + |
| 59 | + assert status.success?, "CLI command failed: #{stderr}" |
| 60 | + assert_match(/language:\s+Ruby/, stdout, "Should detect Ruby language") |
| 61 | + assert_match(/strategy:\s+Extension/, stdout, "Should show Extension strategy") |
| 62 | + end |
| 63 | + |
| 64 | + def test_breakdown_with_gitattributes_strategies |
| 65 | + # Create multiple files with different detection methods |
| 66 | + File.write('.gitattributes', "*.special linguist-language=JavaScript\n") |
| 67 | + File.write('override.special', "console.log('overridden');\n") |
| 68 | + File.write('normal.js', "console.log('normal');\n") |
| 69 | + File.write('Dockerfile', "FROM ubuntu\n") |
| 70 | + |
| 71 | + # Stage and commit the files |
| 72 | + system("git add .") |
| 73 | + system("git commit -m 'Initial commit' --quiet") |
| 74 | + |
| 75 | + # Run github-linguist with --breakdown --strategies flags on the test repository |
| 76 | + stdout, stderr, status = Open3.capture3( |
| 77 | + "bundle", "exec", "github-linguist", @temp_dir, "--breakdown", "--strategies", |
| 78 | + chdir: @original_dir |
| 79 | + ) |
| 80 | + |
| 81 | + assert status.success?, "CLI command failed: #{stderr}" |
| 82 | + |
| 83 | + # Check that GitAttributes strategy appears for the overridden file |
| 84 | + assert_match(/override\.special \[.* \(overridden by \.gitattributes\)\]/, stdout, "Should show override for overridden file") |
| 85 | + |
| 86 | + # Check that normal detection strategies appear for other files |
| 87 | + assert_match(/normal\.js \[Extension\]/, stdout, "Should show Extension strategy for .js file") |
| 88 | + assert_match(/Dockerfile \[Filename\]/, stdout, "Should show Filename strategy for Dockerfile") |
| 89 | + end |
| 90 | + |
| 91 | + def test_json_output_preserves_functionality |
| 92 | + # Create a simple test file |
| 93 | + File.write('test.rb', "puts 'Hello, World!'\n") |
| 94 | + |
| 95 | + # Stage and commit the file |
| 96 | + system("git add .") |
| 97 | + system("git commit -m 'Initial commit' --quiet") |
| 98 | + |
| 99 | + # Run github-linguist with --json flag |
| 100 | + stdout, stderr, status = Open3.capture3( |
| 101 | + "bundle", "exec", "github-linguist", File.join(@temp_dir, "test.rb"), "--json", |
| 102 | + chdir: @original_dir |
| 103 | + ) |
| 104 | + |
| 105 | + assert status.success?, "CLI command failed: #{stderr}" |
| 106 | + |
| 107 | + # Parse JSON output |
| 108 | + require 'json' |
| 109 | + result = JSON.parse(stdout) |
| 110 | + |
| 111 | + test_file_key = File.join(@temp_dir, "test.rb") |
| 112 | + assert_equal "Ruby", result[test_file_key]["language"], "JSON output should contain correct language" |
| 113 | + assert_equal "Text", result[test_file_key]["type"], "JSON output should contain correct type" |
| 114 | + end |
| 115 | + |
| 116 | + def test_repository_scan_with_gitattributes |
| 117 | + # Create a more complex repository structure |
| 118 | + FileUtils.mkdir_p('src') |
| 119 | + File.write('.gitattributes', "*.config linguist-language=JavaScript\n") |
| 120 | + File.write('src/app.rb', "class App\nend\n") |
| 121 | + File.write('config.config', "var x = 1;\n") |
| 122 | + |
| 123 | + # Stage and commit the files |
| 124 | + system("git add .") |
| 125 | + system("git commit -m 'Initial commit' --quiet") |
| 126 | + |
| 127 | + # Run github-linguist on the test repository |
| 128 | + stdout, stderr, status = Open3.capture3( |
| 129 | + "bundle", "exec", "github-linguist", @temp_dir, "--breakdown", "--strategies", |
| 130 | + chdir: @original_dir |
| 131 | + ) |
| 132 | + |
| 133 | + assert status.success?, "CLI command failed: #{stderr}" |
| 134 | + |
| 135 | + # Verify that both normal and override detection work in repository scan |
| 136 | + assert_match(/src\/app\.rb \[Extension\]/, stdout, "Should show Extension strategy for Ruby file") |
| 137 | + assert_match(/config\.config \[.* \(overridden by \.gitattributes\)\]/, stdout, "Should show override for overridden file") |
| 138 | + end |
| 139 | +end |
0 commit comments