Skip to content

Commit e879628

Browse files
committed
Force strategy detection when instrumenter is definded
1 parent 2c0e922 commit e879628

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

lib/linguist/lazy_blob.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,16 @@ def language
7474

7575
@language = if lang = git_attributes['linguist-language']
7676
detected_language = Language.find_by_alias(lang)
77-
# Instrument the gitattributes override as a strategy
78-
if detected_language
79-
strategy = Struct.new(:name).new("Linguist::Strategy::GitAttributes")
77+
78+
# If strategies are being tracked, get the overridden strategy that would have been used with a note that it was overridden
79+
if detected_language && Linguist.instrumenter
80+
# Get the overridden strategy by calling super (which calls Linguist.detect)
81+
super
82+
overridden_strategy_info = Linguist.instrumenter.detected_info[self.name]
83+
overridden_strategy = overridden_strategy_info ? overridden_strategy_info[:strategy] : "Unknown"
84+
85+
strategy_name = "#{overridden_strategy} (overridden by .gitattributes)"
86+
strategy = Struct.new(:name).new(strategy_name)
8087
Linguist.instrument("linguist.detected", blob: self, strategy: strategy, language: detected_language)
8188
end
8289
detected_language

test/test_basic_instrumenter.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ def test_tracks_filename_strategy
8585
def test_tracks_override_strategy
8686
# Simulate a blob with a gitattributes override
8787
blob = Linguist::FileBlob.new("Gemfile", "")
88-
# Simulate detection with gitattributes strategy
89-
strategy = Struct.new(:name).new("Linguist::Strategy::GitAttributes")
88+
# Simulate detection with gitattributes strategy showing the override
89+
strategy = Struct.new(:name).new("Filename (overridden by .gitattributes)")
9090
language = Struct.new(:name).new("Java")
9191
@instrumenter.instrument("linguist.detected", blob: blob, strategy: strategy, language: language) {}
9292
assert @instrumenter.detected_info.key?(blob.name)
93-
assert_equal "GitAttributes", @instrumenter.detected_info[blob.name][:strategy]
93+
assert_match(/overridden by \.gitattributes/, @instrumenter.detected_info[blob.name][:strategy])
9494
assert_equal "Java", @instrumenter.detected_info[blob.name][:language]
9595
end
9696
end

test/test_cli_integration.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def test_strategies_flag_with_gitattributes_override
3939

4040
assert status.success?, "CLI command failed: #{stderr}"
4141
assert_match(/language:\s+Ruby/, stdout, "Should detect Ruby language")
42-
assert_match(/strategy:\s+GitAttributes/, stdout, "Should show GitAttributes strategy")
42+
assert_match(/strategy:\s+.*\(overridden by \.gitattributes\)/, stdout, "Should show override in strategy")
4343
end
4444

4545
def test_strategies_flag_with_normal_detection
@@ -81,7 +81,7 @@ def test_breakdown_with_gitattributes_strategies
8181
assert status.success?, "CLI command failed: #{stderr}"
8282

8383
# Check that GitAttributes strategy appears for the overridden file
84-
assert_match(/override\.special \[GitAttributes\]/, stdout, "Should show GitAttributes strategy for overridden file")
84+
assert_match(/override\.special \[.* \(overridden by \.gitattributes\)\]/, stdout, "Should show override for overridden file")
8585

8686
# Check that normal detection strategies appear for other files
8787
assert_match(/normal\.js \[Extension\]/, stdout, "Should show Extension strategy for .js file")
@@ -134,6 +134,6 @@ def test_repository_scan_with_gitattributes
134134

135135
# Verify that both normal and override detection work in repository scan
136136
assert_match(/src\/app\.rb \[Extension\]/, stdout, "Should show Extension strategy for Ruby file")
137-
assert_match(/config\.config \[GitAttributes\]/, stdout, "Should show GitAttributes strategy for overridden file")
137+
assert_match(/config\.config \[.* \(overridden by \.gitattributes\)\]/, stdout, "Should show override for overridden file")
138138
end
139139
end

0 commit comments

Comments
 (0)