sus 0.37.1 → 0.38.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: adb6aea1bd43dd00163501093a72b5e3ed4e599462248a7aacc4bd98732fd35e
4
- data.tar.gz: 603c5afe02ea770e0826ab158b84543f4b64e2ece0de9c303b8efa22ab092812
3
+ metadata.gz: d3dcb5de6597ebcc7d81b78022f79f4b5de62ec5c902f76bed8f23d2fe9ca8fa
4
+ data.tar.gz: 1eeddde2ea281d7e0d277726905c233de4febb80f5560f03b48b36d6e1d9e0e6
5
5
  SHA512:
6
- metadata.gz: 5c3b7d6b3a32c817fa0b4e473556a272acd573a41d3b0a54ff760208f9037944d8b6c07725e70d96399058cc9e0ae6cf25c3f4aa50714e06aeb5a3b1ecd3f436
7
- data.tar.gz: 11e25dfb46c632ffe7f02ea0a3ccc63b8932749b58cba9564f8d465481b80db4d90504ab1db07e1648a31e7034cd32b99d3a2f9743a35d5032fa750faff2e366
6
+ metadata.gz: 542588a3f55fa3d835ccced647fea9b5469f01c82c79ea2bf5fbaf96f45faf6f98e621a8c1802934c7cc2a56ab763ab4464346606449165fc3321325fb378dee
7
+ data.tar.gz: 2de2a0703afeb06798257b642d507cfee6a2377b6fd23ea391db10bede8b9b093e9f7e9c02a6bdbd2069da0318c089aaea5b16b3dc70033a537f86e6e3268f9f
checksums.yaml.gz.sig CHANGED
Binary file
@@ -198,6 +198,50 @@ end
198
198
 
199
199
  Note the use of `unique: adapter.name` to ensure each test is uniquely identified, which is useful for reporting and debugging - otherwise the same test line number would be used for all iterations, which can make it hard to identify which specific test failed.
200
200
 
201
+ ## Isolated Ruby
202
+
203
+ Use `Sus::Fixtures::IsolatedRubyContext` to evaluate Ruby in a fresh process and assert on its result. This is useful for code that relies on a working directory, environment variables, or constants which must be isolated from other tests.
204
+
205
+ ```ruby
206
+ require "sus/fixtures/isolated_ruby_context"
207
+ require "sus/fixtures/temporary_directory_context"
208
+
209
+ describe "isolated evaluation" do
210
+ include Sus::Fixtures::IsolatedRubyContext
211
+ include Sus::Fixtures::TemporaryDirectoryContext
212
+
213
+ it "reads files in the fixture directory" do
214
+ File.write(File.join(root, "value.txt"), "example")
215
+ result = isolated_ruby(<<~RUBY, chdir: root)
216
+ {value: File.read("value.txt")}
217
+ RUBY
218
+
219
+ expect(result[:value]).to be == "example"
220
+ end
221
+ end
222
+ ```
223
+
224
+ The final expression is returned using `Marshal.dump` and `Marshal.load`, preserving Ruby types, hash keys, string encodings, and shared or cyclic references. The result must support Marshal serialization, and any custom classes it uses must also be loaded in the caller.
225
+
226
+ Exceptions raised while loading requested features, evaluating source, or serializing the result are marshaled back and re-raised in the caller with their original class, message, and backtrace. Custom exception classes must also be loaded in the caller. Returning an exception object as the final expression returns it as a value.
227
+
228
+ Printed output goes to the inherited stderr, keeping it separate from the result. An unsuccessful child that cannot return an exception raises `IsolatedRubyContext::Error`, which exposes its `status`; diagnostics appear directly on stderr. This includes startup failures, unsuccessful explicit exits, and exceptions that cannot be marshaled. A successful exit without a result, such as `exit(0)`, returns `nil`.
229
+
230
+ The fixture uses the current Ruby interpreter and defaults to the caller's working directory. `chdir:` changes only the child's directory, so evaluations can run concurrently. The child inherits the environment, including `RUBYOPT` so coverage and other startup hooks continue to run. `env:` supplies child environment overrides; a nil value removes a variable. For a clean startup without inherited Ruby or Bundler hooks, pass `env: {"RUBYOPT" => nil, "BUNDLER_SETUP" => nil}`.
231
+
232
+ Use `requires:` to load features before evaluating the source. To set up a particular bundle, use an absolute Gemfile path:
233
+
234
+ ```ruby
235
+ result = isolated_ruby(
236
+ 'require "my_gem"; {version: MyGem::VERSION}',
237
+ chdir: root,
238
+ env: {"BUNDLE_GEMFILE" => File.expand_path("gems.rb")},
239
+ requires: ["bundler/setup"]
240
+ )
241
+ ```
242
+
243
+ The fixture accepts source code rather than a block; parent local variables and loaded Ruby state are not transferred to the child. It works independently of `TemporaryDirectoryContext`.
244
+
201
245
  ## Best Practices
202
246
 
203
247
  1. **Organize by domain**: Group related shared contexts together in modules
data/lib/sus/be.rb CHANGED
@@ -46,7 +46,7 @@ module Sus
46
46
  # @parameter other [Object] Another predicate to combine.
47
47
  # @returns [Or] A new OR predicate.
48
48
  def |(other)
49
- Or.new(self, other)
49
+ Or.new([self, other])
50
50
  end
51
51
  end
52
52
 
@@ -93,7 +93,7 @@ module Sus
93
93
  # @parameter other [Object] Another predicate to combine.
94
94
  # @returns [And] A new AND predicate.
95
95
  def &(other)
96
- And.new(self, other)
96
+ And.new([self, other])
97
97
  end
98
98
 
99
99
  # Combine this predicate with another using OR logic.
data/lib/sus/config.rb CHANGED
@@ -27,8 +27,9 @@ module Sus
27
27
  # Load configuration from the given root directory.
28
28
  # @parameter root [String] The root directory to load configuration from.
29
29
  # @parameter arguments [Array] Command line arguments to parse.
30
+ # @parameter env [Hash] The environment to inspect for debug/verbose settings.
30
31
  # @returns [Config] A new Config instance.
31
- def self.load(root: Dir.pwd, arguments: ARGV)
32
+ def self.load(root: Dir.pwd, arguments: ARGV, env: ENV)
32
33
  derived = Class.new(self)
33
34
 
34
35
  if path = self.path(root)
@@ -38,12 +39,40 @@ module Sus
38
39
  end
39
40
 
40
41
  options = {
41
- verbose: !!arguments.delete("--verbose")
42
+ verbose: !!arguments.delete("--verbose") || self.verbose_from_environment?(env)
42
43
  }
43
44
 
44
45
  return derived.new(root, arguments, **options)
45
46
  end
46
47
 
48
+ # Maps CI environment variables to the values they take when the CI provider is running in debug/verbose mode. When any of these match, we enable verbose output automatically.
49
+ #
50
+ # - `RUNNER_DEBUG` is set by GitHub Actions when a workflow is re-run with "Enable debug logging".
51
+ # - `CI_DEBUG_TRACE` is set by GitLab CI when debug logging (tracing) is enabled.
52
+ # - `BUILDKITE_AGENT_DEBUG` is set by Buildkite when agent debug is enabled.
53
+ # - `SYSTEM_DEBUG` is set by Azure Pipelines when the `system.debug` variable is enabled.
54
+ # - `SUS_VERBOSE` can be set explicitly to enable verbose output regardless of the CI provider.
55
+ DEBUG_ENVIRONMENT = {
56
+ "SUS_VERBOSE" => "true",
57
+ "RUNNER_DEBUG" => "1",
58
+ "CI_DEBUG_TRACE" => "true",
59
+ "BUILDKITE_AGENT_DEBUG" => "true",
60
+ "SYSTEM_DEBUG" => "true",
61
+ }
62
+
63
+ # Whether verbose output should be enabled based on the environment.
64
+ #
65
+ # Detects CI environments that request debug logging, e.g. GitHub Actions
66
+ # sets `RUNNER_DEBUG=1` when a workflow is re-run with "Enable debug logging".
67
+ #
68
+ # @parameter env [Hash] The environment to inspect.
69
+ # @returns [Boolean] Whether verbose output should be enabled.
70
+ def self.verbose_from_environment?(env = ENV)
71
+ DEBUG_ENVIRONMENT.any? do |key, value|
72
+ env[key] == value
73
+ end
74
+ end
75
+
47
76
  # Initialize a new Config instance.
48
77
  # @parameter root [String] The root directory for the project.
49
78
  # @parameter paths [Array] Optional paths to specific test files.
@@ -196,13 +225,16 @@ module Sus
196
225
 
197
226
  # Print feedback about the test suite.
198
227
  # @parameter output [Output] The output handler.
199
- # @parameter assertions [Assertions] The assertions instance.
200
- def print_test_feedback(output, assertions)
201
- duration = @clock.duration
202
- rate = assertions.count / duration
203
-
204
- total = assertions.total
205
- count = assertions.count
228
+ # @parameter assertions [Assertions | Nil] The assertions instance.
229
+ # @parameter duration [Float] The total duration of the test suite.
230
+ # @parameter count [Integer] The number of assertions.
231
+ # @parameter total [Integer] The number of tests.
232
+ def print_test_feedback(output, assertions = nil,
233
+ duration: @clock.duration,
234
+ count: assertions.count,
235
+ total: assertions.total
236
+ )
237
+ rate = count / duration
206
238
 
207
239
  if total < 10 or count < 10
208
240
  output.puts "😭 You should write more tests and assertions!"
@@ -212,7 +244,7 @@ module Sus
212
244
  end
213
245
 
214
246
  # Check whether there is at least, on average, one assertion (or more) per test:
215
- assertions_per_test = assertions.count / assertions.total
247
+ assertions_per_test = count / total
216
248
  if assertions_per_test < 1.0
217
249
  output.puts "😩 Your tests don't have enough assertions (#{assertions_per_test.round(1)} < 1.0)!"
218
250
  end
@@ -242,6 +274,8 @@ module Sus
242
274
  end
243
275
  end
244
276
 
277
+ public :print_test_feedback
278
+
245
279
  # Print information about slow tests.
246
280
  # @parameter output [Output] The output handler.
247
281
  # @parameter assertions [Assertions] The assertions instance.
data/lib/sus/context.rb CHANGED
@@ -24,28 +24,6 @@ module Sus
24
24
  base.children = Hash.new
25
25
  end
26
26
 
27
- unless respond_to?(:set_temporary_name)
28
- # Set a temporary name for this context.
29
- # @parameter name [String] The temporary name.
30
- def set_temporary_name(name)
31
- # No-op.
32
- end
33
-
34
- # @returns [String] A string representation of this context.
35
- def to_s
36
- (self.description || self.name).to_s
37
- end
38
-
39
- # @returns [String] An inspect representation of this context.
40
- def inspect
41
- if description = self.description
42
- "\#<#{self.name || "Context"} #{self.description}>"
43
- else
44
- self.name
45
- end
46
- end
47
- end
48
-
49
27
  # Add a child context or test to this context.
50
28
  # @parameter child [Object] The child to add.
51
29
  def add(child)
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2026, by Samuel Williams.
5
+
6
+ require "rbconfig"
7
+
8
+ # @namespace
9
+ module Sus
10
+ # @namespace
11
+ module Fixtures
12
+ # Evaluates Ruby in a fresh process and returns its result through Marshal.
13
+ module IsolatedRubyContext
14
+ # Raised when the child process exits unsuccessfully without returning an exception.
15
+ class Error < RuntimeError
16
+ # @parameter status [Process::Status] The child process's exit status.
17
+ def initialize(status)
18
+ @status = status
19
+ super("Isolated Ruby failed (#{status})")
20
+ end
21
+
22
+ # @attribute [Process::Status] The child process's exit status.
23
+ attr :status
24
+ end
25
+
26
+ # Evaluate source using the current Ruby interpreter, without sharing Ruby state or changing the caller's working directory.
27
+ # The final expression is serialized with Marshal.dump and restored with Marshal.load. Printed output goes to the inherited stderr.
28
+ # Exceptions are marshaled back and re-raised with their original backtraces. SystemExit follows the child process's exit status.
29
+ # @parameter source [String] Ruby source code to evaluate.
30
+ # @parameter chdir [String] The child process's working directory.
31
+ # @parameter env [Hash(String, String | Nil)] Child environment overrides; nil removes a variable. The environment is inherited by default, including RUBYOPT for coverage hooks.
32
+ # @parameter requires [Array(String)] Features to require before evaluating source, such as bundler/setup.
33
+ # @returns [Object] The unmarshaled result, or nil if the child exits successfully without a result. Classes used by the result must be available in the caller.
34
+ # @raises [Exception] The exception raised in the child process, if it can be marshaled back.
35
+ # @raises [Error] If the child exits unsuccessfully without returning an exception.
36
+ # @raises [ArgumentError] If the result or exception uses a class unavailable in the caller.
37
+ def isolated_ruby(source, chdir: Dir.pwd, env: {}, requires: [])
38
+ script = <<~'RUBY'
39
+ ->(output) do
40
+ $stdout.reopen($stderr)
41
+ begin
42
+ source = $stdin.read
43
+ ARGV.each{|feature| require feature}
44
+ result = eval(source, TOPLEVEL_BINDING, File.join(Dir.pwd, "(isolated ruby)"))
45
+ output.write(Marshal.dump([result, nil]))
46
+ rescue SystemExit
47
+ raise
48
+ rescue Exception => error
49
+ output.write(Marshal.dump([nil, error]))
50
+ end
51
+ end.call($stdout.dup.binmode)
52
+ RUBY
53
+
54
+ output = IO.popen([env, RbConfig.ruby, "-e", script, "--", *requires], "r+b", chdir: chdir) do |process|
55
+ begin
56
+ process.write(source)
57
+ rescue Errno::EPIPE
58
+ # A startup failure may close stdin before accepting the source:
59
+ end
60
+ process.close_write
61
+ process.read
62
+ end
63
+ status = $?
64
+ raise Error.new(status) unless status.success?
65
+ return nil if output.empty?
66
+
67
+ result, error = Marshal.load(output)
68
+ raise error if error
69
+ result
70
+ end
71
+ end
72
+ end
73
+ end
@@ -4,6 +4,7 @@
4
4
  # Copyright, 2025-2026, by Samuel Williams.
5
5
 
6
6
  require "tmpdir"
7
+ require "fileutils"
7
8
 
8
9
  module Sus
9
10
  module Fixtures
@@ -12,10 +13,17 @@ module Sus
12
13
  # Set up a temporary directory before the test and clean it up after.
13
14
  # @yields {|&block| ...} The test block to execute.
14
15
  def around(&block)
15
- Dir.mktmpdir do |root|
16
+ root = Dir.mktmpdir
17
+
18
+ begin
16
19
  @root = root
20
+
17
21
  super(&block)
22
+ ensure
18
23
  @root = nil
24
+
25
+ # Use forced removal so cleanup tolerates paths which were already removed by the test or an external process.
26
+ FileUtils.remove_entry(root, true)
19
27
  end
20
28
  end
21
29
 
@@ -24,4 +32,3 @@ module Sus
24
32
  end
25
33
  end
26
34
  end
27
-
data/lib/sus/identity.rb CHANGED
@@ -142,7 +142,7 @@ module Sus
142
142
  end
143
143
  else
144
144
  # In theory this should be a bit faster:
145
- each_caller_location do |location|
145
+ Thread.each_caller_location do |location|
146
146
  if location.path == @path
147
147
  return self.with_line(location.lineno)
148
148
  end
@@ -154,16 +154,6 @@ module Sus
154
154
 
155
155
  protected
156
156
 
157
- if Thread.respond_to?(:each_caller_location)
158
- def each_caller_location(&block)
159
- Thread.each_caller_location(&block)
160
- end
161
- else
162
- def each_caller_location(&block)
163
- caller_locations(1).each(&block)
164
- end
165
- end
166
-
167
157
  def append_unique_key(key, unique = @unique)
168
158
  if @parent
169
159
  @parent.append_unique_key(key)
data/lib/sus/it.rb CHANGED
@@ -107,17 +107,19 @@ module Sus
107
107
 
108
108
  # Skip the test unless the Ruby version meets the minimum requirement.
109
109
  # @parameter version [String] The minimum Ruby version required.
110
- def skip_unless_minimum_ruby_version(version)
111
- unless RUBY_VERSION >= version
112
- skip "Ruby #{version} is required, but running #{RUBY_VERSION}!"
110
+ # @parameter ruby_version [String] The Ruby version to check.
111
+ def skip_unless_minimum_ruby_version(version, ruby_version = RUBY_VERSION)
112
+ unless compare_ruby_version(ruby_version, version) >= 0
113
+ skip "Ruby #{version} is required, but running #{ruby_version}!"
113
114
  end
114
115
  end
115
116
 
116
117
  # Skip the test if the Ruby version exceeds the maximum supported version.
117
118
  # @parameter version [String] The maximum Ruby version supported.
118
- def skip_if_maximum_ruby_version(version)
119
- if RUBY_VERSION >= version
120
- skip "Ruby #{version} is not supported, but running #{RUBY_VERSION}!"
119
+ # @parameter ruby_version [String] The Ruby version to check.
120
+ def skip_if_maximum_ruby_version(version, ruby_version = RUBY_VERSION)
121
+ if compare_ruby_version(ruby_version, version) >= 0
122
+ skip "Ruby #{version} is not supported, but running #{ruby_version}!"
121
123
  end
122
124
  end
123
125
 
@@ -128,5 +130,23 @@ module Sus
128
130
  skip "Ruby platform #{match} is not supported!"
129
131
  end
130
132
  end
133
+
134
+ private
135
+
136
+ def compare_ruby_version(left, right)
137
+ left_segments = left.split(".").map(&:to_i)
138
+ right_segments = right.split(".").map(&:to_i)
139
+ length = [left_segments.size, right_segments.size].max
140
+
141
+ length.times do |index|
142
+ left_segment = left_segments[index] || 0
143
+ right_segment = right_segments[index] || 0
144
+
145
+ return -1 if left_segment < right_segment
146
+ return 1 if left_segment > right_segment
147
+ end
148
+
149
+ return 0
150
+ end
131
151
  end
132
152
  end
@@ -21,9 +21,9 @@ module Sus
21
21
  parameters = @parameters.dup
22
22
 
23
23
  assertions.nested(self) do |assertions|
24
- expected_name = parameters.shift
25
-
26
24
  subject.each do |type, name|
25
+ expected_name = parameters.shift
26
+
27
27
  case type
28
28
  when :req
29
29
  assertions.assert(name == expected_name, "parameter #{expected_name} is required, but was #{name}")
data/lib/sus/version.rb CHANGED
@@ -5,5 +5,5 @@
5
5
 
6
6
  # @namespace
7
7
  module Sus
8
- VERSION = "0.37.1"
8
+ VERSION = "0.38.0"
9
9
  end
data/readme.md CHANGED
@@ -33,6 +33,14 @@ Please see the [project documentation](https://socketry.github.io/sus/) for more
33
33
 
34
34
  Please see the [project releases](https://socketry.github.io/sus/releases/index) for all releases.
35
35
 
36
+ ### v0.38.0
37
+
38
+ - Add `Sus::Fixtures::IsolatedRubyContext#isolated_ruby` for evaluating Ruby in a fresh process with optional working directory and environment overrides, returning Ruby values and re-raising exceptions in the caller.
39
+
40
+ ### v0.37.2
41
+
42
+ - Make `Sus::Fixtures::TemporaryDirectoryContext` ignore temporary directory cleanup failures.
43
+
36
44
  ### v0.37.1
37
45
 
38
46
  - Fixed `Sus::Mock#wrap` to forward blocks to the original method, and fixed `receive(...).with_block(...)` to use the supplied predicate.
data/releases.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Releases
2
2
 
3
+ ## v0.38.0
4
+
5
+ - Add `Sus::Fixtures::IsolatedRubyContext#isolated_ruby` for evaluating Ruby in a fresh process with optional working directory and environment overrides, returning Ruby values and re-raising exceptions in the caller.
6
+
7
+ ## v0.37.2
8
+
9
+ - Make `Sus::Fixtures::TemporaryDirectoryContext` ignore temporary directory cleanup failures.
10
+
3
11
  ## v0.37.1
4
12
 
5
13
  - Fixed `Sus::Mock#wrap` to forward blocks to the original method, and fixed `receive(...).with_block(...)` to use the supplied predicate.
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.37.1
4
+ version: 0.38.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -70,6 +70,7 @@ files:
70
70
  - lib/sus/file.rb
71
71
  - lib/sus/filter.rb
72
72
  - lib/sus/fixtures.rb
73
+ - lib/sus/fixtures/isolated_ruby_context.rb
73
74
  - lib/sus/fixtures/temporary_directory_context.rb
74
75
  - lib/sus/have.rb
75
76
  - lib/sus/have/all.rb
@@ -127,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
128
  - !ruby/object:Gem::Version
128
129
  version: '0'
129
130
  requirements: []
130
- rubygems_version: 4.0.10
131
+ rubygems_version: 4.0.16
131
132
  specification_version: 4
132
133
  summary: A fast and scalable test runner.
133
134
  test_files: []
metadata.gz.sig CHANGED
Binary file