Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Add simple spec for logical coupling extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
floriandejonckheere committed Mar 31, 2024
1 parent 1b109af commit 8871075
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 7 deletions.
8 changes: 4 additions & 4 deletions lib/mosaik/extractors/evolution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ def call
git = Git.open(options[:directory], log: ::Logger.new(File::NULL))

# Fetch commits, limited to the last N commits
commits = git.log(options[:limit]) if options[:limit]
commits = git.log(options[:limit])

# Limit commits to the load paths
commits = commits.path(MOSAIK.configuration.load_paths.map { |l| File.join(options[:directory], l) })
commits = commits.path(MOSAIK.configuration.load_paths)

# Limit commits to a specific date
commits = commits.since(options[:since]) if options[:since]
Expand All @@ -38,9 +38,9 @@ def call
# Get the files for the commit
files = commit.diff_parent.stats[:files]

# Reject files not in the load paths
# Select only included files
files = files
.map { |file, _| File.join(MOSAIK.options.directory, file) }
.map { |file, _| File.join(options[:directory], file) }
.select { |file| file.in? MOSAIK.configuration.files }

# Resolve file paths to class name
Expand Down
28 changes: 25 additions & 3 deletions spec/mosaik/extractors/evolution_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
RSpec.describe MOSAIK::Extractors::Evolution do
subject(:extractor) { described_class.new(options, graph) }

let(:options) { { directory: MOSAIK.root, logical: 1, contributor: 1 } }
let(:options) { { directory: MOSAIK.root, limit: 100, logical: 1, contributor: 1 } }
let(:graph) { build(:graph) }

let(:configuration) { build(:configuration) }

describe "#validate" do
it "does not raise an error" do
expect { extractor.validate }.not_to raise_error
Expand All @@ -21,4 +19,28 @@
end
end
end

context "when the weights are zero" do
let(:options) { { logical: 0, contributor: 0 } }

it "does not extract evolutionary coupling information" do
expect { extractor.call }.not_to(change { graph.vertices.count })
end
end

describe "logical coupling" do
let(:options) { { directory:, limit: 100, logical: 1, contributor: 0 } }

include_context "with a git repository"

it "constructs a logical coupling graph" do
extractor.call

# Extract all vertices with source and destination
expect(graph.vertices.transform_values { |v| v.edges.keys }).to eq(
"App::Foo" => ["App::Bar"],
"App::Bar" => ["App::Foo"],
)
end
end
end
43 changes: 43 additions & 0 deletions spec/support/shared_contexts/repository.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

RSpec.shared_context "with a git repository" do
let(:directory) { Dir.mktmpdir }
let(:git) { Git.init(directory) }

let(:configuration) { MOSAIK::Configuration.from(File.join(directory, "mosaik.yml")) }

before do
# Setup the repository with initial commit
File.write(File.join(directory, "README.md"), "# Test Repository")
git.add
git.commit("Initial commit", author: "Author 1 <[email protected]>")

# Add MOSAIK configuration
FileUtils.cp(MOSAIK.root.join("config/mosaik.yml"), File.join(directory, "mosaik.yml"))
git.add
git.commit("Add MOSAIK configuration", author: "Author 1 <[email protected]>")

# Add application structure
FileUtils.mkdir(File.join(directory, "lib"))
File.write(File.join(directory, "lib/app.rb"), "class App; end")
git.add
git.commit("Set up application structure", author: "Author 1 <[email protected]>")

# Add classes
FileUtils.mkdir(File.join(directory, "lib", "app"))
File.write(File.join(directory, "lib/app/foo.rb"), "class App::Foo; end")
File.write(File.join(directory, "lib/app/bar.rb"), "class App::Bar; end")
git.add
git.commit("Set up application structure", author: "Author 1 <[email protected]>")

# Mock the configuration
allow(MOSAIK)
.to receive(:configuration)
.and_return configuration
end

after do
# Cleanup the temporary directory
FileUtils.remove_entry(directory)
end
end

0 comments on commit 8871075

Please sign in to comment.