From 1b109af974cdb1cea24a36f9fd1fde2acc44fe66 Mon Sep 17 00:00:00 2001 From: Florian Dejonckheere Date: Sun, 31 Mar 2024 14:07:45 +0300 Subject: [PATCH] Ensure directory is a git root --- lib/mosaik/commands/extract.rb | 2 ++ lib/mosaik/extractor.rb | 2 ++ lib/mosaik/extractors/evolution.rb | 4 ++++ spec/mosaik/extractors/evolution_spec.rb | 24 ++++++++++++++++++++++++ 4 files changed, 32 insertions(+) create mode 100644 spec/mosaik/extractors/evolution_spec.rb diff --git a/lib/mosaik/commands/extract.rb b/lib/mosaik/commands/extract.rb index 4d47770..6784992 100644 --- a/lib/mosaik/commands/extract.rb +++ b/lib/mosaik/commands/extract.rb @@ -41,11 +41,13 @@ def call # Extract structural coupling information and add to graph Extractors::Structural .new(options, graph) + .tap(&:validate) .call # Extract evolutionary (logical and contributor) coupling information and add to graph Extractors::Evolution .new(options, graph) + .tap(&:validate) .call end diff --git a/lib/mosaik/extractor.rb b/lib/mosaik/extractor.rb index 3d918d6..d3592c0 100644 --- a/lib/mosaik/extractor.rb +++ b/lib/mosaik/extractor.rb @@ -12,6 +12,8 @@ def initialize(options, graph) @graph = graph end + def validate; end + def call raise NotImplementedError end diff --git a/lib/mosaik/extractors/evolution.rb b/lib/mosaik/extractors/evolution.rb index 859dce6..3956179 100644 --- a/lib/mosaik/extractors/evolution.rb +++ b/lib/mosaik/extractors/evolution.rb @@ -6,6 +6,10 @@ module Extractors # Evolutionary (logical and contributor) coupling extraction # class Evolution < Extractor + def validate + raise OptionError, "directory is not a git repository" unless File.directory?(File.join(options[:directory], ".git")) + end + def call return unless options[:logical].positive? || options[:contributor].positive? diff --git a/spec/mosaik/extractors/evolution_spec.rb b/spec/mosaik/extractors/evolution_spec.rb new file mode 100644 index 0000000..3e3e249 --- /dev/null +++ b/spec/mosaik/extractors/evolution_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +RSpec.describe MOSAIK::Extractors::Evolution do + subject(:extractor) { described_class.new(options, graph) } + + let(:options) { { directory: MOSAIK.root, 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 + end + + context "when the directory is not a git repository root" do + let(:options) { { directory: "/tmp" } } + + it "raises an error" do + expect { extractor.validate }.to raise_error MOSAIK::OptionError, "directory is not a git repository" + end + end + end +end