Skip to content

Commit ecea1b4

Browse files
committed
Add more tests and cleanup comments
1 parent 7b4fcd3 commit ecea1b4

File tree

10 files changed

+96
-25
lines changed

10 files changed

+96
-25
lines changed

.rubocop.yml

-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ Style/Documentation:
4141
Style/SignalException:
4242
Enabled: false
4343

44-
Style/FrozenStringLiteralComment:
45-
Enabled: false
46-
4744
Style/MultilineBlockChain:
4845
Enabled: false
4946

lib/query_packwerk.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
require 'parse_packwerk'
66
require 'rubocop'
77

8-
#
98
# QueryPackwerk is a tool for querying Packwerk violations.
109
#
1110
# It is built on top of ParsePackwerk, and provides a Ruby-friendly API
1211
# for querying package.yml and package_todo.yml files.
1312
#
13+
# See also: https://github.com/rubyatscale/parse_packwerk
1414
module QueryPackwerk
1515
autoload :Console, 'query_packwerk/console'
1616
autoload :ConsoleHelpers, 'query_packwerk/console_helpers'
@@ -24,8 +24,8 @@ module QueryPackwerk
2424
autoload :Version, 'query_packwerk/version'
2525

2626
extend T::Sig
27-
# TODO: module_function isn't playing nicely with Sorbet
28-
extend self # rubocop:todo Style/ModuleFunction
27+
28+
module_function
2929

3030
sig { params(name: String).returns(T.nilable(QueryPackwerk::Package)) }
3131
def package(name)

lib/query_packwerk/cli.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# typed: strict
2+
# frozen_string_literal: true
23

34
require 'thor'
45

lib/query_packwerk/console_helpers.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# typed: true
2+
# frozen_string_literal: true
23

34
module QueryPackwerk
45
module ConsoleHelpers
56
extend T::Sig
7+
include Kernel
68

79
sig { returns(String) }
810
def inspect

lib/query_packwerk/packages.rb

+17-8
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,17 @@ def empty?
6161
@original_collection.empty?
6262
end
6363

64-
sig { params(arg0: T.proc.params(arg0: QueryPackwerk::Package).returns(T::Boolean)).returns(T::Boolean) }
65-
def any?(&block)
66-
@original_collection.any?(&block)
64+
sig do
65+
params(
66+
blk: T.nilable(T.proc.params(arg0: QueryPackwerk::Package).returns(T::Boolean))
67+
).returns(T::Boolean)
68+
end
69+
def any?(&blk)
70+
if blk.nil?
71+
!empty?
72+
else
73+
@original_collection.any?(&blk)
74+
end
6775
end
6876

6977
# You can query for packages rather than violations to get a broader view, and
@@ -78,11 +86,12 @@ def violations
7886

7987
sig { returns(String) }
8088
def inspect
81-
[
82-
"#<#{self.class.name} [",
83-
to_a.map(&:inspect).join("\n"),
84-
']>'
85-
].join("\n")
89+
arr = to_a.map(&:inspect)
90+
if arr.empty?
91+
"#<#{self.class.name} []>"
92+
else
93+
"#<#{self.class.name} [\n#{arr.join("\n")}\n]>"
94+
end
8695
end
8796
end
8897
end

lib/query_packwerk/query_interface.rb

-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ def each(&block)
3636
end
3737

3838
# Should be overridden, the base of most of the queries.
39-
#
40-
# TODO: Consider refactoring to `abstract` interface
4139
sig { overridable.returns(T::Array[T.untyped]) }
4240
def original_collection
4341
[]

lib/query_packwerk/violation.rb

+1-4
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def files
8888

8989
# Whether or not the files containing violations match any provided globs
9090
#
91-
# See also: https://ruby-doc.org/core-2.7.6/File.html#method-c-fnmatch
91+
# See Ruby documentation for File.fnmatch for more details.
9292
sig { params(globs: T.any(String, Regexp)).returns(T::Boolean) }
9393
def includes_files?(*globs)
9494
globs.any? do |glob|
@@ -272,9 +272,6 @@ def deconstruct_keys(keys)
272272
**runtime_keys(keys)
273273
}
274274

275-
# all_values[:is_active_record] = active_record? if !keys || keys.include?(:is_active_record)
276-
# all_values[:is_constant] = active_record? if !keys || keys.include?(:is_constant)
277-
278275
keys.nil? ? all_values : all_values.slice(*T.unsafe(keys))
279276
end
280277

lib/query_packwerk/violations.rb

+6-5
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,12 @@ def excluding_files(*file_globs)
263263

264264
sig { returns(String) }
265265
def inspect
266-
[
267-
"#<#{self.class.name} [",
268-
to_a.map(&:inspect).join("\n"),
269-
']>'
270-
].join("\n")
266+
arr = to_a.map(&:inspect)
267+
if arr.empty?
268+
"#<#{self.class.name} []>"
269+
else
270+
"#<#{self.class.name} [\n#{arr.join("\n")}\n]>"
271+
end
271272
end
272273

273274
private

spec/query_packwerk/packages_spec.rb

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# typed: false
2+
# frozen_string_literal: true
3+
4+
RSpec.describe QueryPackwerk::Packages do
5+
include_context 'pseudo packs'
6+
7+
describe '#where' do
8+
it 'can query packages' do
9+
packages = described_class.where(owner: QueryPackwerk::Packages::UNOWNED)
10+
expect(packages).to be_a(described_class)
11+
expect(packages.map(&:owner).uniq).to eq([QueryPackwerk::Packages::UNOWNED])
12+
end
13+
end
14+
15+
describe '#empty? and #any?' do
16+
it 'returns true for empty? when there are no packages' do
17+
empty_packages = described_class.new([])
18+
expect(empty_packages.empty?).to be true
19+
expect(empty_packages.any?).to be false
20+
end
21+
22+
it 'returns false for empty? when there are packages' do
23+
expect(described_class.all.empty?).to be false
24+
expect(described_class.all.any?).to be true
25+
end
26+
27+
it 'supports any? with a block' do
28+
packages = described_class.all
29+
expect(packages.any? { |p| p.owner == QueryPackwerk::Packages::UNOWNED }).to be true
30+
expect(packages.any? { |p| p.name == 'nonexistent' }).to be false
31+
end
32+
end
33+
34+
describe '#violations' do
35+
it 'returns violations for all packages in the collection' do
36+
packages = described_class.all
37+
violations = packages.violations
38+
expect(violations).to be_a(QueryPackwerk::Violations)
39+
expect(violations.count).to be > 0
40+
end
41+
end
42+
43+
describe '#inspect' do
44+
it 'returns a string representation of an empty collection' do
45+
packages = described_class.new([])
46+
expect(packages.inspect).to eq("#<#{described_class.name} []>")
47+
end
48+
49+
it 'returns a string representation of the packages' do
50+
packages = described_class.all
51+
expect(packages.inspect).to eq("#<#{described_class.name} [\n#{described_class.all.map(&:inspect).join("\n")}\n]>")
52+
end
53+
end
54+
end

spec/query_packwerk/violations_spec.rb

+12
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@
3131
end
3232
end
3333

34+
describe '#inspect' do
35+
it 'returns a string representation of an empty collection' do
36+
violations = described_class.new([])
37+
expect(violations.inspect).to eq("#<#{described_class.name} []>")
38+
end
39+
40+
it 'returns a string representation of a collection with violations' do
41+
violations = described_class.all
42+
expect(violations.inspect).to eq("#<#{described_class.name} [\n#{described_class.all.map(&:inspect).join("\n")}\n]>")
43+
end
44+
end
45+
3446
describe '#anonymous_sources_with_locations' do
3547
it 'can get anonymized sources with their file locations' do
3648
# { constant => { violating code shape => [where it happened] } }

0 commit comments

Comments
 (0)