|
| 1 | +# typed: true |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require "test_helper" |
| 5 | +require "support/rails_test_helper" |
| 6 | + |
| 7 | +module Packwerk |
| 8 | + class CustomExecutableIntegrationTest < Minitest::Test |
| 9 | + include ApplicationFixtureHelper |
| 10 | + |
| 11 | + TIMELINE_PATH = Pathname.new("components/timeline") |
| 12 | + |
| 13 | + setup do |
| 14 | + reset_output |
| 15 | + setup_application_fixture |
| 16 | + use_template(:skeleton) |
| 17 | + end |
| 18 | + |
| 19 | + teardown do |
| 20 | + teardown_application_fixture |
| 21 | + end |
| 22 | + |
| 23 | + test "'packwerk check' with no violations succeeds in all variants" do |
| 24 | + assert_successful_run("check") |
| 25 | + assert_match(/No offenses detected/, captured_output) |
| 26 | + |
| 27 | + reset_output |
| 28 | + assert_successful_run(["check", "components/timeline"]) |
| 29 | + assert_match(/No offenses detected/, captured_output) |
| 30 | + |
| 31 | + reset_output |
| 32 | + assert_successful_run(["check", "--packages=components/timeline"]) |
| 33 | + assert_match(/No offenses detected/, captured_output) |
| 34 | + end |
| 35 | + |
| 36 | + test "'packwerk check' with violations only in nested packages has different outcomes per variant" do |
| 37 | + open_app_file(TIMELINE_PATH.join("nested", "timeline_comment.rb")) do |file| |
| 38 | + file.write("class TimelineComment; belongs_to :order, class_name: '::Order'; end") |
| 39 | + file.flush |
| 40 | + end |
| 41 | + |
| 42 | + refute_successful_run("check") |
| 43 | + assert_match(/Dependency violation: ::Order/, captured_output) |
| 44 | + assert_match(/1 offense detected/, captured_output) |
| 45 | + |
| 46 | + reset_output |
| 47 | + refute_successful_run(["check", "components/timeline"]) |
| 48 | + assert_match(/Dependency violation: ::Order/, captured_output) |
| 49 | + assert_match(/1 offense detected/, captured_output) |
| 50 | + |
| 51 | + reset_output |
| 52 | + assert_successful_run(["check", "--packages=components/timeline"]) |
| 53 | + assert_match(/No offenses detected/, captured_output) |
| 54 | + end |
| 55 | + |
| 56 | + test "'packwerk check' with failures in different parts of the app has different outcomes per variant" do |
| 57 | + open_app_file(TIMELINE_PATH.join("nested", "timeline_comment.rb")) do |file| |
| 58 | + file.write("class TimelineComment; belongs_to :order, class_name: '::Order'; end") |
| 59 | + file.flush |
| 60 | + end |
| 61 | + |
| 62 | + refute_successful_run("check") |
| 63 | + assert_match(/Dependency violation: ::Order/, captured_output) |
| 64 | + assert_match(/1 offense detected/, captured_output) |
| 65 | + |
| 66 | + reset_output |
| 67 | + assert_successful_run(["check", "components/sales"]) |
| 68 | + assert_match(/No offenses detected/, captured_output) |
| 69 | + |
| 70 | + reset_output |
| 71 | + assert_successful_run(["check", "--packages=components/sales"]) |
| 72 | + assert_match(/No offenses detected/, captured_output) |
| 73 | + end |
| 74 | + |
| 75 | + test "'packwerk update-todo' with no violations succeeds and updates no files" do |
| 76 | + package_todo_content = read_package_todo |
| 77 | + |
| 78 | + assert_successful_run("update-todo") |
| 79 | + |
| 80 | + package_todo_content_after_update = read_package_todo |
| 81 | + expected_output = <<~EOS |
| 82 | + 📦 Packwerk is inspecting 12 files |
| 83 | + \\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\. |
| 84 | + 📦 Finished in \\d+\\.\\d+ seconds |
| 85 | +
|
| 86 | + No offenses detected |
| 87 | + ✅ `package_todo.yml` has been updated. |
| 88 | + EOS |
| 89 | + |
| 90 | + assert_equal(package_todo_content, package_todo_content_after_update, |
| 91 | + "expected no updates to any package todo file") |
| 92 | + assert_match(/#{expected_output}/, captured_output) |
| 93 | + end |
| 94 | + |
| 95 | + test "'packwerk update-todo' with violations succeeds and updates relevant package_todo" do |
| 96 | + package_todo_content = read_package_todo |
| 97 | + timeline_package_todo_path = to_app_path(File.join(TIMELINE_PATH, "package_todo.yml")) |
| 98 | + |
| 99 | + open_app_file(TIMELINE_PATH.join("app", "models", "timeline_comment.rb")) do |file| |
| 100 | + file.write("class TimelineComment; belongs_to :order; end") |
| 101 | + file.flush |
| 102 | + |
| 103 | + assert_successful_run("update-todo") |
| 104 | + |
| 105 | + assert(File.exist?(timeline_package_todo_path), |
| 106 | + "expected new package_todo for timeline package to be created") |
| 107 | + |
| 108 | + timeline_package_todo_content = File.read(timeline_package_todo_path) |
| 109 | + assert_match( |
| 110 | + "components/sales:\n \"::Order\":\n violations:\n - dependency", |
| 111 | + timeline_package_todo_content |
| 112 | + ) |
| 113 | + |
| 114 | + package_todo_content_after_update = |
| 115 | + read_package_todo.reject { |k, _v| k.match?(timeline_package_todo_path) } |
| 116 | + expected_output = <<~EOS |
| 117 | + 📦 Packwerk is inspecting 13 files |
| 118 | + \\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\. |
| 119 | + 📦 Finished in \\d+\\.\\d+ seconds |
| 120 | +
|
| 121 | + No offenses detected |
| 122 | + ✅ `package_todo.yml` has been updated. |
| 123 | + EOS |
| 124 | + |
| 125 | + assert_equal(package_todo_content, package_todo_content_after_update, |
| 126 | + "expected no updates to any package todo files besides timeline/package_todo.yml") |
| 127 | + assert_match(/#{expected_output}/, captured_output) |
| 128 | + end |
| 129 | + end |
| 130 | + |
| 131 | + private |
| 132 | + |
| 133 | + def assert_successful_run(command) |
| 134 | + Packwerk::Cli.new(out: @out).run(Array(command)) |
| 135 | + rescue SystemExit => e |
| 136 | + assert_equal(0, e.status) |
| 137 | + end |
| 138 | + |
| 139 | + def refute_successful_run(command) |
| 140 | + Packwerk::Cli.new(out: @out).run(Array(command)) |
| 141 | + rescue SystemExit => e |
| 142 | + refute_equal(0, e.status) |
| 143 | + end |
| 144 | + |
| 145 | + def reset_output |
| 146 | + @out = StringIO.new |
| 147 | + end |
| 148 | + |
| 149 | + def captured_output |
| 150 | + @out.string |
| 151 | + end |
| 152 | + |
| 153 | + def read_package_todo |
| 154 | + package_todo_glob = File.join("**", "package_todo.yml") |
| 155 | + package_todo_files = Dir.glob(package_todo_glob) |
| 156 | + Hash[ |
| 157 | + package_todo_files.collect do |file| |
| 158 | + [to_app_path(file), File.read(file)] |
| 159 | + end |
| 160 | + ] |
| 161 | + end |
| 162 | + end |
| 163 | +end |
0 commit comments