Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/cc/engine/analyzers/analyzer_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ def process_file(path)
def file_list
@_file_list ||= ::CC::Engine::Analyzers::FileList.new(
engine_config: engine_config,
default_paths: self.class::DEFAULT_PATHS,
language: self.class::LANGUAGE
patterns: self.class::PATTERNS,
)
end
end
Expand Down
4 changes: 0 additions & 4 deletions lib/cc/engine/analyzers/engine_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ def mass_threshold_for(language)
end
end

def paths_for(language)
fetch_language(language).fetch("paths", nil)
end

private

attr_reader :config
Expand Down
67 changes: 25 additions & 42 deletions lib/cc/engine/analyzers/file_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,46 @@ module CC
module Engine
module Analyzers
class FileList
def initialize(engine_config:, default_paths:, language:)
def initialize(engine_config:, patterns:)
@engine_config = engine_config
@default_paths = default_paths
@language = language
@patterns = patterns
end

def files
Array(matching_files) & Array(included_files)
end

private

attr_reader :engine_config, :default_paths, :language

def matching_files
paths.map do |glob|
Dir.glob("./#{glob}").reject do |path|
File.directory?(path)
engine_config.include_paths.flat_map do |path|
if path.end_with?("/")
expand(path)
elsif matches?(path)
[path]
else
[]
end
end.flatten
end
end

def paths
engine_paths || default_paths
end
private

def engine_paths
@engine_config.paths_for(language)
end
attr_reader :engine_config, :patterns

def included_files
include_paths.
map { |path| make_relative(path) }.
map { |path| collect_files(path) }.flatten.compact
end
def expand(path)
globs = patterns.map { |p| File.join(relativize(path), p) }

def collect_files(path)
if File.directory?(path)
Dir.entries(path).map do |new_path|
next if [".", ".."].include?(new_path)
collect_files File.join(path, new_path)
end
else
path
end
Dir.glob(globs)
end

def make_relative(path)
if path.match(%r(^\./))
path
else
"./#{path}"
def matches?(path)
patterns.any? do |p|
File.fnmatch?(
relativize(p),
relativize(path),
File::FNM_PATHNAME | File::FNM_EXTGLOB
)
end
end

def include_paths
engine_config.include_paths
# Ensure all paths (and patterns) are ./-prefixed
def relativize(path)
"./#{path.sub(%r{^\./}, "")}"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/cc/engine/analyzers/javascript/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Engine
module Analyzers
module Javascript
class Main < CC::Engine::Analyzers::Base
DEFAULT_PATHS = [
PATTERNS = [
"**/*.js",
"**/*.jsx"
]
Expand Down
2 changes: 1 addition & 1 deletion lib/cc/engine/analyzers/php/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Analyzers
module Php
class Main < CC::Engine::Analyzers::Base
LANGUAGE = "php"
DEFAULT_PATHS = [
PATTERNS = [
"**/*.php",
"**/*.inc",
"**/*.module"
Expand Down
2 changes: 1 addition & 1 deletion lib/cc/engine/analyzers/python/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Analyzers
module Python
class Main < CC::Engine::Analyzers::Base
LANGUAGE = "python"
DEFAULT_PATHS = ["**/*.py"]
PATTERNS = ["**/*.py"]
DEFAULT_MASS_THRESHOLD = 32
POINTS_PER_OVERAGE = 50_000

Expand Down
2 changes: 1 addition & 1 deletion lib/cc/engine/analyzers/ruby/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Analyzers
module Ruby
class Main < CC::Engine::Analyzers::Base
LANGUAGE = "ruby"
DEFAULT_PATHS = [
PATTERNS = [
"**/*.rb",
"**/*.rake",
"**/Rakefile",
Expand Down
30 changes: 1 addition & 29 deletions spec/cc/engine/analyzers/engine_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,6 @@
end
end

describe "#paths_for" do
it "returns paths values for given language" do
engine_config = CC::Engine::Analyzers::EngineConfig.new({
"config" => {
"languages" => {
"EliXiR" => {
"paths" => ["/", "/etc"],
}
}
}
})

expect(engine_config.paths_for("elixir")).to eq(["/", "/etc"])
end

it "returns nil if language is an empty key" do
engine_config = CC::Engine::Analyzers::EngineConfig.new({
"config" => {
"languages" => {
"EliXiR" => ""
}
}
})

expect(engine_config.paths_for("elixir")).to be_nil
end
end

describe "mass_threshold_for" do
it "returns configured mass threshold as integer" do
engine_config = CC::Engine::Analyzers::EngineConfig.new({
Expand Down Expand Up @@ -103,7 +75,7 @@
end
end

describe "exlude_paths" do
describe "include_paths" do
it "returns given include paths" do
engine_config = CC::Engine::Analyzers::EngineConfig.new({
"include_paths" => ["/tmp"]
Expand Down
61 changes: 11 additions & 50 deletions spec/cc/engine/analyzers/file_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,65 +21,26 @@
end

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

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

it "returns files from engine config defined paths when present" do
it "filters file includes by patterns" do
file_list = ::CC::Engine::Analyzers::FileList.new(
engine_config: CC::Engine::Analyzers::EngineConfig.new({
"config" => {
"languages" => {
"elixir" => {
"paths" => ["**/*.ex"]
}
}
}
}),
default_paths: ["**/*.js", "**/*.jsx"],
language: "elixir",
engine_config: CC::Engine::Analyzers::EngineConfig.new(
"include_paths" => ["./foo.ex", "./foo.js"],
),
patterns: ["**/*.js", "**/*.jsx"],
)

expect(file_list.files).to eq(["./foo.ex"])
end

it "returns files from default_paths when languages is an array" do
file_list = ::CC::Engine::Analyzers::FileList.new(
engine_config: CC::Engine::Analyzers::EngineConfig.new({
"config" => {
"languages" => [
"elixir"
],
},
}),
default_paths: ["**/*.js", "**/*.jsx"],
language: "javascript",
)

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

it "excludes files not in include_paths" do
file_list = ::CC::Engine::Analyzers::FileList.new(
engine_config: CC::Engine::Analyzers::EngineConfig.new({
"include_paths" => ["foo.jsx", "nested"],
"config" => {
"languages" => [
"elixir"
],
},
}),
default_paths: ["**/*.js", "**/*.jsx", "**/*.hs"],
language: "javascript",
)

expect(file_list.files).to eq(["./foo.jsx", "./nested/nest.hs"])
expect(file_list.files).to eq(["./foo.js"])
end
end
end