Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit 72c3a8b

Browse files
committed
Merge pull request #108 from codeclimate/pb-include-paths
Simplify FileList building
2 parents bff607c + da38684 commit 72c3a8b

File tree

9 files changed

+42
-131
lines changed

9 files changed

+42
-131
lines changed

‎lib/cc/engine/analyzers/analyzer_base.rb‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ def process_file(path)
6262
def file_list
6363
@_file_list ||= ::CC::Engine::Analyzers::FileList.new(
6464
engine_config: engine_config,
65-
default_paths: self.class::DEFAULT_PATHS,
66-
language: self.class::LANGUAGE
65+
patterns: self.class::PATTERNS,
6766
)
6867
end
6968
end

‎lib/cc/engine/analyzers/engine_config.rb‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ def mass_threshold_for(language)
2626
end
2727
end
2828

29-
def paths_for(language)
30-
fetch_language(language).fetch("paths", nil)
31-
end
32-
3329
private
3430

3531
attr_reader :config

‎lib/cc/engine/analyzers/file_list.rb‎

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,63 +4,46 @@ module CC
44
module Engine
55
module Analyzers
66
class FileList
7-
def initialize(engine_config:, default_paths:,language:)
7+
def initialize(engine_config:, patterns:)
88
@engine_config = engine_config
9-
@default_paths = default_paths
10-
@language = language
9+
@patterns = patterns
1110
end
1211

1312
def files
14-
Array(matching_files) & Array(included_files)
15-
end
16-
17-
private
18-
19-
attr_reader :engine_config, :default_paths, :language
20-
21-
def matching_files
22-
paths.map do |glob|
23-
Dir.glob("./#{glob}").reject do |path|
24-
File.directory?(path)
13+
engine_config.include_paths.flat_map do |path|
14+
if path.end_with?("/")
15+
expand(path)
16+
elsif matches?(path)
17+
[path]
18+
else
19+
[]
2520
end
26-
end.flatten
21+
end
2722
end
2823

29-
def paths
30-
engine_paths || default_paths
31-
end
24+
private
3225

33-
def engine_paths
34-
@engine_config.paths_for(language)
35-
end
26+
attr_reader :engine_config, :patterns
3627

37-
def included_files
38-
include_paths.
39-
map { |path| make_relative(path) }.
40-
map { |path| collect_files(path) }.flatten.compact
41-
end
28+
def expand(path)
29+
globs = patterns.map { |p| File.join(relativize(path), p) }
4230

43-
def collect_files(path)
44-
if File.directory?(path)
45-
Dir.entries(path).map do |new_path|
46-
next if [".", ".."].include?(new_path)
47-
collect_files File.join(path, new_path)
48-
end
49-
else
50-
path
51-
end
31+
Dir.glob(globs)
5232
end
5333

54-
def make_relative(path)
55-
if path.match(%r(^\./))
56-
path
57-
else
58-
"./#{path}"
34+
def matches?(path)
35+
patterns.any? do |p|
36+
File.fnmatch?(
37+
relativize(p),
38+
relativize(path),
39+
File::FNM_PATHNAME | File::FNM_EXTGLOB
40+
)
5941
end
6042
end
6143

62-
def include_paths
63-
engine_config.include_paths
44+
# Ensure all paths (and patterns) are ./-prefixed
45+
def relativize(path)
46+
"./#{path.sub(%r{^\./}, "")}"
6447
end
6548
end
6649
end

‎lib/cc/engine/analyzers/javascript/main.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Engine
1010
module Analyzers
1111
module Javascript
1212
class Main < CC::Engine::Analyzers::Base
13-
DEFAULT_PATHS = [
13+
PATTERNS = [
1414
"**/*.js",
1515
"**/*.jsx"
1616
]

‎lib/cc/engine/analyzers/php/main.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module Analyzers
99
module Php
1010
class Main < CC::Engine::Analyzers::Base
1111
LANGUAGE = "php"
12-
DEFAULT_PATHS = [
12+
PATTERNS = [
1313
"**/*.php",
1414
"**/*.inc",
1515
"**/*.module"

‎lib/cc/engine/analyzers/python/main.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module Analyzers
1111
module Python
1212
class Main < CC::Engine::Analyzers::Base
1313
LANGUAGE = "python"
14-
DEFAULT_PATHS = ["**/*.py"]
14+
PATTERNS = ["**/*.py"]
1515
DEFAULT_MASS_THRESHOLD = 32
1616
POINTS_PER_OVERAGE = 50_000
1717

‎lib/cc/engine/analyzers/ruby/main.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module Analyzers
99
module Ruby
1010
class Main < CC::Engine::Analyzers::Base
1111
LANGUAGE = "ruby"
12-
DEFAULT_PATHS = [
12+
PATTERNS = [
1313
"**/*.rb",
1414
"**/*.rake",
1515
"**/Rakefile",

‎spec/cc/engine/analyzers/engine_config_spec.rb‎

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -47,34 +47,6 @@
4747
end
4848
end
4949

50-
describe "#paths_for" do
51-
it "returns paths values for given language" do
52-
engine_config = CC::Engine::Analyzers::EngineConfig.new({
53-
"config" => {
54-
"languages" => {
55-
"EliXiR" => {
56-
"paths" => ["/", "/etc"],
57-
}
58-
}
59-
}
60-
})
61-
62-
expect(engine_config.paths_for("elixir")).to eq(["/", "/etc"])
63-
end
64-
65-
it "returns nil if language is an empty key" do
66-
engine_config = CC::Engine::Analyzers::EngineConfig.new({
67-
"config" => {
68-
"languages" => {
69-
"EliXiR" => ""
70-
}
71-
}
72-
})
73-
74-
expect(engine_config.paths_for("elixir")).to be_nil
75-
end
76-
end
77-
7850
describe "mass_threshold_for" do
7951
it "returns configured mass threshold as integer" do
8052
engine_config = CC::Engine::Analyzers::EngineConfig.new({
@@ -103,7 +75,7 @@
10375
end
10476
end
10577

106-
describe "exlude_paths" do
78+
describe "include_paths" do
10779
it "returns given include paths" do
10880
engine_config = CC::Engine::Analyzers::EngineConfig.new({
10981
"include_paths" => ["/tmp"]

‎spec/cc/engine/analyzers/file_list_spec.rb‎

Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,65 +21,26 @@
2121
end
2222

2323
describe "#files" do
24-
it "returns files from default_paths when language is missing paths" do
24+
it "expands patterns for directory includes" do
2525
file_list = ::CC::Engine::Analyzers::FileList.new(
26-
engine_config: CC::Engine::Analyzers::EngineConfig.new({}),
27-
default_paths: ["**/*.js", "**/*.jsx"],
28-
language: "javascript",
26+
engine_config: CC::Engine::Analyzers::EngineConfig.new(
27+
"include_paths" => ["./"],
28+
),
29+
patterns: ["**/*.js", "**/*.jsx"],
2930
)
3031

3132
expect(file_list.files).to eq(["./foo.js", "./foo.jsx"])
3233
end
3334

34-
it "returns files from engine config defined paths when present" do
35+
it "filters file includes by patterns" do
3536
file_list = ::CC::Engine::Analyzers::FileList.new(
36-
engine_config: CC::Engine::Analyzers::EngineConfig.new({
37-
"config" => {
38-
"languages" => {
39-
"elixir" => {
40-
"paths" => ["**/*.ex"]
41-
}
42-
}
43-
}
44-
}),
45-
default_paths: ["**/*.js", "**/*.jsx"],
46-
language: "elixir",
37+
engine_config: CC::Engine::Analyzers::EngineConfig.new(
38+
"include_paths" => ["./foo.ex", "./foo.js"],
39+
),
40+
patterns: ["**/*.js", "**/*.jsx"],
4741
)
4842

49-
expect(file_list.files).to eq(["./foo.ex"])
50-
end
51-
52-
it "returns files from default_paths when languages is an array" do
53-
file_list = ::CC::Engine::Analyzers::FileList.new(
54-
engine_config: CC::Engine::Analyzers::EngineConfig.new({
55-
"config" => {
56-
"languages" => [
57-
"elixir"
58-
],
59-
},
60-
}),
61-
default_paths: ["**/*.js", "**/*.jsx"],
62-
language: "javascript",
63-
)
64-
65-
expect(file_list.files).to eq(["./foo.js", "./foo.jsx"])
66-
end
67-
68-
it "excludes files not in include_paths" do
69-
file_list = ::CC::Engine::Analyzers::FileList.new(
70-
engine_config: CC::Engine::Analyzers::EngineConfig.new({
71-
"include_paths" => ["foo.jsx", "nested"],
72-
"config" => {
73-
"languages" => [
74-
"elixir"
75-
],
76-
},
77-
}),
78-
default_paths: ["**/*.js", "**/*.jsx", "**/*.hs"],
79-
language: "javascript",
80-
)
81-
82-
expect(file_list.files).to eq(["./foo.jsx", "./nested/nest.hs"])
43+
expect(file_list.files).to eq(["./foo.js"])
8344
end
8445
end
8546
end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /