Skip to content

Commit

Permalink
add test for cli.rb testing detect_stale_violations
Browse files Browse the repository at this point in the history
  • Loading branch information
cindygshopify committed Nov 9, 2020
1 parent 063d616 commit 5634f4f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
12 changes: 6 additions & 6 deletions lib/packwerk/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ def execute_command(args)
end
end

private

def init
@out.puts("📦 Initializing Packwerk...")

Expand Down Expand Up @@ -180,11 +178,11 @@ def check(paths)
all_offenses.empty?
end

def detect_stale_violations(paths)
detect_stale_deprecated_references = ::Packwerk::DetectStaleDeprecatedReferences.new(@configuration.root_path)
def detect_stale_violations(paths,
reference_lister: ::Packwerk::DetectStaleDeprecatedReferences.new(@configuration.root_path))
@run_context = Packwerk::RunContext.from_configuration(
@configuration,
reference_lister: detect_stale_deprecated_references
reference_lister: reference_lister
)

files = fetch_files_to_process(paths)
Expand All @@ -198,7 +196,7 @@ def detect_stale_violations(paths)
end
end

status = !detect_stale_deprecated_references.stale_violations?
status = !reference_lister.stale_violations?
msg = status ? "No stale violations detected" : "There were stale violations produced, please run packwerk update"

@out.puts
Expand All @@ -207,6 +205,8 @@ def detect_stale_violations(paths)
status
end

private

def fetch_files_to_process(paths)
files = FilesForProcessing.fetch(paths: paths, configuration: @configuration)
abort("No files found or given. "\
Expand Down
5 changes: 3 additions & 2 deletions lib/packwerk/run_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ class RunContext
:context_provider,
:root_path,
:file_processor,
:node_processor_class,
:reference_lister
:node_processor_class
)

attr_accessor :reference_lister

DEFAULT_CHECKERS = [
::Packwerk::DependencyChecker,
::Packwerk::PrivacyChecker,
Expand Down
32 changes: 32 additions & 0 deletions test/unit/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,38 @@ class CliTest < Minitest::Test
refute success
end

test "#execute_command with the subcommand detect-stale-violations returns status code 1 if stale violations" do
stale_violations_message = "There were stale violations produced, please run packwerk update"
offense = stub
detect_stale_deprecated_references = stub
detect_stale_deprecated_references.stubs(:stale_violations?).returns(true)

#::Packwerk::DetectStaleDeprecatedReferences.any_instance.stubs(:stale_violations?).returns(true)

file_processor = stub
file_processor
.stubs(:call)
.returns([offense])

run_context = stub
run_context.stubs(:file_processor).at_least_once.returns(file_processor)

string_io = StringIO.new

cli = ::Packwerk::Cli.new(out: string_io, run_context: run_context)

::Packwerk::FilesForProcessing.stubs(fetch: ["path/of/exile.rb"])
::Packwerk::RunContext.stubs(from_configuration: run_context)

no_stale_violations = cli.detect_stale_violations(
"path/of/exile.rb",
reference_lister: detect_stale_deprecated_references
)

assert_includes string_io.string, stale_violations_message
refute no_stale_violations
end

test "#execute_command with the subcommand help lists all the valid subcommands" do
@cli.execute_command(["help"])

Expand Down

0 comments on commit 5634f4f

Please sign in to comment.