rubocop-hgoostdd 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4c1ae10a637f90f9e429e8dc4cd890a7a6e69d9391a21d9a0cd709e492fd4832
4
+ data.tar.gz: edfd60cef5f12f859139a297500f756fe588d9baf4d45ba6229c8a189478a916
5
+ SHA512:
6
+ metadata.gz: 2fad4f9674daeceb4516d6847756120d85cd67c4f6e8d8916bb6279ad022b32630c1c4e1479b5557fe67a3bd4de240f6ce2fd813bbba3b887cef6201205b29ff
7
+ data.tar.gz: adfd8413938df19b45ef5b1e0d74366e3d05d2537dea9a77f87b1ee87cebaf008bfb442e32afc1b74f5187d8a7fd3f0137482f65e89b16d68b5b2b42a8884ce3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Tommy Caruso
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # rubocop-hgoostdd
2
+
3
+ Custom RuboCop cops that codify the rules of **Hardened GOOS TDD** —
4
+ the testing discipline that wraps Freeman & Pryce's outside-in TDD with
5
+ explicit guardrails against the failure modes that bite long-lived
6
+ mockist codebases: interface drift, behavior drift, hidden globals, and
7
+ ossified mocks.
8
+
9
+ ## What it enforces
10
+
11
+ | Cop | Rule | Autocorrect |
12
+ |---|---|---|
13
+ | `HGOOSTDD/NoTimeInDomain` | §6 — no `Time.now` / `Time.current` in domain code | `→ @clock.now` (unsafe) |
14
+ | `HGOOSTDD/NoSecureRandomInDomain` | §6 — no `SecureRandom.*` in domain code | `→ @id_gen.<method>(args)` (unsafe) |
15
+ | `HGOOSTDD/NoRailsLoggerInDomain` | §6 — no `Rails.logger` in domain code | `→ @logger` preserving chain (unsafe) |
16
+ | `HGOOSTDD/NoEnvOutsideAppConfig` | §6 — `ENV[` / `ENV.fetch` only in `app/support/app_config.rb` | — |
17
+ | `HGOOSTDD/NoTimecopInDomainSpecs` | §6 — no `Timecop` / `travel_to` in inner-loop specs | — |
18
+ | `HGOOSTDD/NoMocksInAcceptance` | §2 — no `instance_double` / `allow` / `expect…to receive` in `spec/acceptance/**` | — |
19
+
20
+ The three autocorrectable cops are marked `SafeAutoCorrect: false` —
21
+ the rewrite assumes the surrounding object has the matching ivar wired
22
+ by an injected collaborator. `rubocop -a` skips them; only `rubocop -A`
23
+ applies.
24
+
25
+ ## Install
26
+
27
+ ```ruby
28
+ # Gemfile
29
+ group :development, :test do
30
+ gem "rubocop-hgoostdd", require: false
31
+ end
32
+ ```
33
+
34
+ ```yaml
35
+ # .rubocop.yml
36
+ require:
37
+ - rubocop-hgoostdd
38
+
39
+ AllCops:
40
+ TargetRubyVersion: 4.0
41
+ NewCops: disable
42
+ DisabledByDefault: true
43
+ SuggestExtensions: false
44
+ ```
45
+
46
+ `DisabledByDefault: true` is recommended unless you're also running
47
+ stock RuboCop — otherwise default style cops will fire and bury the
48
+ HGOOSTDD output.
49
+
50
+ ## Default paths
51
+
52
+ Each cop ships with Rails-app-shaped `Include`/`Exclude` defaults under
53
+ `config/default.yml`. Override in your own `.rubocop.yml` if your
54
+ project structure differs:
55
+
56
+ ```yaml
57
+ HGOOSTDD/NoTimeInDomain:
58
+ Include:
59
+ - 'lib/**/*'
60
+ Exclude:
61
+ - 'lib/clock.rb'
62
+ ```
63
+
64
+ ## The six rules in brief
65
+
66
+ - **§1** Only mock types you own. Wrap third-party SDKs in adapters; mock the adapter, not the SDK.
67
+ - **§2** Outer-loop acceptance test against real Rails; inner loop mockist and fast.
68
+ - **§3** Contract tests for adapters — paired against real and fake.
69
+ - **§4** Inject collaborators; no hidden reaches.
70
+ - **§5** Real > Fake > Stub > Mock. Fakes are first-class.
71
+ - **§6** No implicit globals in domain code.
72
+
73
+ Full discipline at the goos_tdd worked example (the sandbox that
74
+ produced this gem).
75
+
76
+ ## Origin
77
+
78
+ These cops were extracted from a Rails sandbox built to validate the
79
+ discipline end-to-end (`goos_tdd`). Each cop has a paired
80
+ `expect_offense` / `expect_no_offenses` spec; the three autocorrect
81
+ cops also assert via `expect_correction`. 24 examples, 0 failures.
82
+
83
+ ## License
84
+
85
+ MIT.
@@ -0,0 +1,63 @@
1
+ AllCops:
2
+ DisabledByDefault: true
3
+
4
+ HGOOSTDD/NoTimeInDomain:
5
+ Description: 'Inject a clock: collaborator; do not call Time.now / Time.current in domain code (§6).'
6
+ Enabled: true
7
+ SafeAutoCorrect: false
8
+ Include:
9
+ - 'app/services/**/*'
10
+ - 'app/domain/**/*'
11
+ - 'app/controllers/**/*'
12
+ - 'app/models/**/*'
13
+ - 'app/adapters/**/*'
14
+ - 'app/repositories/**/*'
15
+ - 'app/jobs/**/*'
16
+ Exclude:
17
+ - 'app/support/clock.rb'
18
+
19
+ HGOOSTDD/NoSecureRandomInDomain:
20
+ Description: 'Inject an id_gen: collaborator; do not call SecureRandom.* in domain code (§6).'
21
+ Enabled: true
22
+ SafeAutoCorrect: false
23
+ Include:
24
+ - 'app/services/**/*'
25
+ - 'app/domain/**/*'
26
+ - 'app/controllers/**/*'
27
+ - 'app/models/**/*'
28
+ - 'app/adapters/**/*'
29
+ - 'app/repositories/**/*'
30
+ - 'app/jobs/**/*'
31
+ Exclude:
32
+ - 'app/support/id_gen.rb'
33
+
34
+ HGOOSTDD/NoRailsLoggerInDomain:
35
+ Description: 'Inject a logger: collaborator; do not reach for Rails.logger in domain code (§6).'
36
+ Enabled: true
37
+ SafeAutoCorrect: false
38
+ Include:
39
+ - 'app/**/*'
40
+ Exclude:
41
+ - 'app/support/**/*'
42
+
43
+ HGOOSTDD/NoEnvOutsideAppConfig:
44
+ Description: 'Read configuration through AppConfig, not directly from ENV (§6).'
45
+ Enabled: true
46
+ Include:
47
+ - 'app/**/*'
48
+ Exclude:
49
+ - 'app/support/app_config.rb'
50
+
51
+ HGOOSTDD/NoTimecopInDomainSpecs:
52
+ Description: 'Use a FakeClock instead of Timecop / travel_to in domain specs (§6).'
53
+ Enabled: true
54
+ Include:
55
+ - 'spec/**/*'
56
+ Exclude:
57
+ - 'spec/acceptance/**/*'
58
+
59
+ HGOOSTDD/NoMocksInAcceptance:
60
+ Description: 'Acceptance specs must not use mocks; swap fakes via the adapter registry (§2).'
61
+ Enabled: true
62
+ Include:
63
+ - 'spec/acceptance/**/*'
@@ -0,0 +1,26 @@
1
+ module RuboCop
2
+ module Cop
3
+ module HGOOSTDD
4
+ # `ENV[...]` and `ENV.fetch` should only appear in the AppConfig
5
+ # boundary. Domain code reads config through an injected `config:`
6
+ # collaborator. See HGOOSTDD §6.
7
+ class NoEnvOutsideAppConfig < Base
8
+ MSG = "Read configuration through AppConfig, not directly from ENV (§6)."
9
+
10
+ def_node_matcher :env_index?, <<~PATTERN
11
+ (send (const {nil? cbase} :ENV) :[] _)
12
+ PATTERN
13
+
14
+ def_node_matcher :env_fetch?, <<~PATTERN
15
+ (send (const {nil? cbase} :ENV) :fetch ...)
16
+ PATTERN
17
+
18
+ def on_send(node)
19
+ return unless env_index?(node) || env_fetch?(node)
20
+
21
+ add_offense(node, message: MSG)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,36 @@
1
+ module RuboCop
2
+ module Cop
3
+ module HGOOSTDD
4
+ # Acceptance specs exercise the real Rails stack with fakes only at
5
+ # the adapter seam. They must not mock or stub. Inner-loop unit
6
+ # specs can use mocks; acceptance specs cannot. See HGOOSTDD §2.
7
+ class NoMocksInAcceptance < Base
8
+ MSG = "Acceptance specs must not use `%{call}`; swap fakes via the adapter registry instead (§2)."
9
+
10
+ MOCK_BUILDERS = %i[instance_double class_double object_double].freeze
11
+
12
+ def_node_matcher :mock_builder?, <<~PATTERN
13
+ (send nil? ${:instance_double :class_double :object_double} ...)
14
+ PATTERN
15
+
16
+ def_node_matcher :allow_call?, <<~PATTERN
17
+ (send nil? :allow _)
18
+ PATTERN
19
+
20
+ def_node_matcher :receive_chain?, <<~PATTERN
21
+ (send (send nil? :expect _) :to (send nil? :receive _))
22
+ PATTERN
23
+
24
+ def on_send(node)
25
+ if (m = mock_builder?(node))
26
+ add_offense(node, message: format(MSG, call: m))
27
+ elsif allow_call?(node)
28
+ add_offense(node, message: format(MSG, call: :allow))
29
+ elsif receive_chain?(node)
30
+ add_offense(node, message: format(MSG, call: "expect(...).to receive"))
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,29 @@
1
+ module RuboCop
2
+ module Cop
3
+ module HGOOSTDD
4
+ # Domain code logs through an injected `logger:` collaborator, never
5
+ # the global `Rails.logger`. See HGOOSTDD §6.
6
+ #
7
+ # Autocorrect rewrites `Rails.logger` (the receiver) to `@logger`,
8
+ # leaving any chained method call intact. Marked unsafe — assumes
9
+ # `@logger` exists.
10
+ class NoRailsLoggerInDomain < Base
11
+ extend AutoCorrector
12
+
13
+ MSG = "Inject a `logger:` collaborator; do not reach for `Rails.logger` in domain code (§6)."
14
+
15
+ def_node_matcher :rails_logger?, <<~PATTERN
16
+ (send (const {nil? cbase} :Rails) :logger)
17
+ PATTERN
18
+
19
+ def on_send(node)
20
+ return unless rails_logger?(node)
21
+
22
+ add_offense(node, message: MSG) do |corrector|
23
+ corrector.replace(node, "@logger")
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,32 @@
1
+ module RuboCop
2
+ module Cop
3
+ module HGOOSTDD
4
+ # Domain code must generate IDs via an injected `id_gen:` collaborator,
5
+ # not via the `SecureRandom` constant. See HGOOSTDD §6.
6
+ #
7
+ # Autocorrect rewrites `SecureRandom.<method>(args)` to
8
+ # `@id_gen.<method>(args)`, preserving the method name and args.
9
+ # Marked unsafe — assumes `@id_gen` exists and exposes a matching
10
+ # method (e.g. `IdGen#uuid` for `SecureRandom.uuid`).
11
+ class NoSecureRandomInDomain < Base
12
+ extend AutoCorrector
13
+
14
+ MSG = "Inject an `id_gen:` collaborator; do not call `%{call}` in domain code (§6)."
15
+
16
+ def_node_matcher :secure_random_send?, <<~PATTERN
17
+ (send (const {nil? cbase} :SecureRandom) $_ ...)
18
+ PATTERN
19
+
20
+ def on_send(node)
21
+ return unless (method_name = secure_random_send?(node))
22
+
23
+ add_offense(node, message: format(MSG, call: node.source)) do |corrector|
24
+ args = node.arguments.map(&:source).join(", ")
25
+ replacement = args.empty? ? "@id_gen.#{method_name}" : "@id_gen.#{method_name}(#{args})"
26
+ corrector.replace(node, replacement)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,33 @@
1
+ module RuboCop
2
+ module Cop
3
+ module HGOOSTDD
4
+ # Domain code must read time via an injected `clock:` collaborator,
5
+ # not via `Time.now` / `Time.current`. See HGOOSTDD §6.
6
+ #
7
+ # Autocorrect rewrites the call to `@clock.now`. This is marked
8
+ # unsafe (SafeAutoCorrect: false in .rubocop.yml) because it
9
+ # assumes the surrounding object has a `@clock` ivar wired by an
10
+ # injected collaborator. After autocorrect, the developer must
11
+ # ensure the constructor accepts `clock:` and assigns `@clock`.
12
+ class NoTimeInDomain < Base
13
+ extend AutoCorrector
14
+
15
+ MSG = "Inject a `clock:` collaborator; do not call `%{call}` in domain code (§6)."
16
+
17
+ RESTRICT_ON_SEND = %i[now current].freeze
18
+
19
+ def_node_matcher :time_send?, <<~PATTERN
20
+ (send (const {nil? cbase} :Time) {:now :current})
21
+ PATTERN
22
+
23
+ def on_send(node)
24
+ return unless time_send?(node)
25
+
26
+ add_offense(node, message: format(MSG, call: node.source)) do |corrector|
27
+ corrector.replace(node, "@clock.now")
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,31 @@
1
+ module RuboCop
2
+ module Cop
3
+ module HGOOSTDD
4
+ # `Timecop`, `travel_to`, `travel_back`, and `freeze_time` are
5
+ # acceptable in acceptance specs (which exercise the real Rails
6
+ # stack) but not in inner-loop domain specs — inject a clock fake
7
+ # instead. See HGOOSTDD §6.
8
+ class NoTimecopInDomainSpecs < Base
9
+ MSG = "Use a FakeClock instead of `%{call}` in domain specs (§6)."
10
+
11
+ TRAVEL_METHODS = %i[travel_to travel_back freeze_time travel].freeze
12
+
13
+ def_node_matcher :timecop_send?, <<~PATTERN
14
+ (send (const {nil? cbase} :Timecop) _ ...)
15
+ PATTERN
16
+
17
+ def_node_matcher :travel_call?, <<~PATTERN
18
+ (send nil? ${:travel_to :travel_back :freeze_time :travel} ...)
19
+ PATTERN
20
+
21
+ def on_send(node)
22
+ if timecop_send?(node)
23
+ add_offense(node, message: format(MSG, call: node.source))
24
+ elsif travel_call?(node)
25
+ add_offense(node, message: format(MSG, call: node.method_name))
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,24 @@
1
+ require "pathname"
2
+ require "yaml"
3
+
4
+ module RuboCop
5
+ module HGOOSTDD
6
+ PROJECT_ROOT = Pathname.new(__dir__).parent.parent.parent.expand_path.freeze
7
+ CONFIG_DEFAULT = PROJECT_ROOT.join("config", "default.yml").freeze
8
+
9
+ # Merges this gem's default.yml into RuboCop's default configuration so
10
+ # host projects only need `require: rubocop-hgoostdd` in their
11
+ # .rubocop.yml and can rely on the per-cop Include/Exclude/Enabled
12
+ # defaults shipped here.
13
+ module Inject
14
+ def self.defaults!
15
+ path = CONFIG_DEFAULT.to_s
16
+ hash = ConfigLoader.send(:load_yaml_configuration, path)
17
+ config = RuboCop::Config.new(hash, path)
18
+ puts "configuration from #{path}" if ConfigLoader.debug?
19
+ config = ConfigLoader.merge_with_default(config, path)
20
+ ConfigLoader.instance_variable_set(:@default_configuration, config)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ module RuboCop
2
+ module HGOOSTDD
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,13 @@
1
+ require "rubocop"
2
+
3
+ require_relative "rubocop/hgoostdd/version"
4
+ require_relative "rubocop/hgoostdd/inject"
5
+
6
+ RuboCop::HGOOSTDD::Inject.defaults!
7
+
8
+ require_relative "rubocop/cop/hgoostdd/no_time_in_domain"
9
+ require_relative "rubocop/cop/hgoostdd/no_secure_random_in_domain"
10
+ require_relative "rubocop/cop/hgoostdd/no_rails_logger_in_domain"
11
+ require_relative "rubocop/cop/hgoostdd/no_env_outside_app_config"
12
+ require_relative "rubocop/cop/hgoostdd/no_timecop_in_domain_specs"
13
+ require_relative "rubocop/cop/hgoostdd/no_mocks_in_acceptance"
@@ -0,0 +1,32 @@
1
+ require_relative "lib/rubocop/hgoostdd/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "rubocop-hgoostdd"
5
+ spec.version = RuboCop::HGOOSTDD::VERSION
6
+ spec.authors = ["Tommy Caruso"]
7
+ spec.email = ["tommy.caruso2118@gmail.com"]
8
+
9
+ spec.summary = "RuboCop cops enforcing Hardened GOOS TDD discipline"
10
+ spec.description = <<~DESC
11
+ Six cops that codify the §1-§6 rules of Hardened GOOS TDD:
12
+ no Time.now / SecureRandom / Rails.logger / ENV reads in domain code,
13
+ no Timecop in inner-loop specs, no mocks in acceptance specs.
14
+ Three of the six autocorrect to the injected-collaborator form.
15
+ DESC
16
+ spec.homepage = "https://github.com/tommy2118/rubocop-hgoostdd"
17
+ spec.license = "MIT"
18
+ spec.required_ruby_version = ">= 3.0"
19
+
20
+ spec.metadata["homepage_uri"] = spec.homepage
21
+
22
+ spec.files = Dir[
23
+ "lib/**/*.rb",
24
+ "config/**/*.yml",
25
+ "LICENSE.txt",
26
+ "README.md",
27
+ "rubocop-hgoostdd.gemspec"
28
+ ]
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_dependency "rubocop", ">= 1.70", "< 2.0"
32
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubocop-hgoostdd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tommy Caruso
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rubocop
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '1.70'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '1.70'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '2.0'
32
+ description: |
33
+ Six cops that codify the §1-§6 rules of Hardened GOOS TDD:
34
+ no Time.now / SecureRandom / Rails.logger / ENV reads in domain code,
35
+ no Timecop in inner-loop specs, no mocks in acceptance specs.
36
+ Three of the six autocorrect to the injected-collaborator form.
37
+ email:
38
+ - tommy.caruso2118@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - LICENSE.txt
44
+ - README.md
45
+ - config/default.yml
46
+ - lib/rubocop-hgoostdd.rb
47
+ - lib/rubocop/cop/hgoostdd/no_env_outside_app_config.rb
48
+ - lib/rubocop/cop/hgoostdd/no_mocks_in_acceptance.rb
49
+ - lib/rubocop/cop/hgoostdd/no_rails_logger_in_domain.rb
50
+ - lib/rubocop/cop/hgoostdd/no_secure_random_in_domain.rb
51
+ - lib/rubocop/cop/hgoostdd/no_time_in_domain.rb
52
+ - lib/rubocop/cop/hgoostdd/no_timecop_in_domain_specs.rb
53
+ - lib/rubocop/hgoostdd/inject.rb
54
+ - lib/rubocop/hgoostdd/version.rb
55
+ - rubocop-hgoostdd.gemspec
56
+ homepage: https://github.com/tommy2118/rubocop-hgoostdd
57
+ licenses:
58
+ - MIT
59
+ metadata:
60
+ homepage_uri: https://github.com/tommy2118/rubocop-hgoostdd
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubygems_version: 4.0.10
76
+ specification_version: 4
77
+ summary: RuboCop cops enforcing Hardened GOOS TDD discipline
78
+ test_files: []