scout-essentials 1.9.1 → 1.9.4

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.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/.vimproject +19 -1
  3. data/Rakefile +9 -0
  4. data/VERSION +1 -1
  5. data/doc/Improvements.md +20 -0
  6. data/doc/StartHere.md +7 -4
  7. data/doc/developer/AnnotationSystem.md +10 -15
  8. data/doc/developer/Configuration.md +20 -30
  9. data/doc/developer/CoreUtilities.md +52 -22
  10. data/doc/developer/DesignPrinciples.md +7 -9
  11. data/doc/developer/DocumentationPractices.md +476 -0
  12. data/doc/developer/ErrorHandling.md +11 -20
  13. data/doc/developer/LockingAndConcurrency.md +20 -13
  14. data/doc/developer/StreamingModel.md +13 -14
  15. data/doc/developer/Testing.md +389 -0
  16. data/doc/user/AnnotatingData.md +9 -13
  17. data/doc/user/CommandLineOptions.md +9 -12
  18. data/doc/user/Cookbook.md +7 -10
  19. data/doc/user/HandlingStreams.md +22 -29
  20. data/doc/user/LoggingAndProgress.md +9 -11
  21. data/doc/user/ProducingResources.md +0 -1
  22. data/doc/user/RemoteData.md +10 -13
  23. data/doc/user/RunningCommands.md +12 -17
  24. data/doc/user/WorkingWithFiles.md +10 -2
  25. data/lib/scout/config.rb +1 -0
  26. data/lib/scout/indiferent_hash/json.rb +5 -0
  27. data/lib/scout/indiferent_hash.rb +10 -3
  28. data/lib/scout/log/fingerprint.rb +1 -1
  29. data/lib/scout/misc/digest.rb +11 -6
  30. data/lib/scout/named_array.rb +4 -0
  31. data/lib/scout/open/final.rb +1 -1
  32. data/lib/scout/open/stream.rb +14 -13
  33. data/lib/scout/path/util.rb +1 -1
  34. data/lib/scout/testing/availability.rb +264 -0
  35. data/lib/scout/testing/scout_state.rb +193 -0
  36. data/lib/scout/testing.rb +82 -0
  37. data/research/doc-audit-findings.md +7 -5
  38. data/research/doc-structure.md +89 -0
  39. data/research/lock-namespace-blindness.md +73 -0
  40. data/research/probing-guide.md +311 -0
  41. data/research/testing-availability-scout-ai-handoff.md +235 -0
  42. data/research/testing-availability-scout-rig-handoff.md +355 -0
  43. data/scout-essentials.gemspec +26 -3
  44. data/test/scout/annotation/test_dup_clone.rb +31 -0
  45. data/test/scout/indiferent_hash/test_case_insensitive_staleness.rb +27 -0
  46. data/test/scout/indiferent_hash/test_json.rb +13 -0
  47. data/test/scout/indiferent_hash/test_string2hash_false.rb +13 -0
  48. data/test/scout/misc/test_timespan.rb +30 -0
  49. data/test/scout/open/test_compression_detection.rb +32 -0
  50. data/test/scout/open/test_forced_gzip_broken.rb +25 -0
  51. data/test/scout/test_named_array_resolution.rb +49 -0
  52. data/test/scout/test_testing.rb +195 -0
  53. data/test/scout/test_tmpfile_cleanup.rb +23 -0
  54. data/test/scout/testing/test_availability.rb +222 -0
  55. data/test/scout/testing/test_state_bindings.rb +163 -0
  56. data/test/test_helper.rb +26 -12
  57. metadata +25 -2
@@ -2,11 +2,11 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Juwelier::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: scout-essentials 1.9.1 ruby lib
5
+ # stub: scout-essentials 1.9.4 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "scout-essentials".freeze
9
- s.version = "1.9.1".freeze
9
+ s.version = "1.9.4".freeze
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib".freeze]
@@ -33,11 +33,13 @@ Gem::Specification.new do |s|
33
33
  "doc/developer/Configuration.md",
34
34
  "doc/developer/CoreUtilities.md",
35
35
  "doc/developer/DesignPrinciples.md",
36
+ "doc/developer/DocumentationPractices.md",
36
37
  "doc/developer/ErrorHandling.md",
37
38
  "doc/developer/LockingAndConcurrency.md",
38
39
  "doc/developer/PathResolution.md",
39
40
  "doc/developer/PersistenceAndResources.md",
40
41
  "doc/developer/StreamingModel.md",
42
+ "doc/developer/Testing.md",
41
43
  "doc/user/AnnotatingData.md",
42
44
  "doc/user/CachingResults.md",
43
45
  "doc/user/CommandLineOptions.md",
@@ -59,6 +61,7 @@ Gem::Specification.new do |s|
59
61
  "lib/scout/exceptions.rb",
60
62
  "lib/scout/indiferent_hash.rb",
61
63
  "lib/scout/indiferent_hash/case_insensitive.rb",
64
+ "lib/scout/indiferent_hash/json.rb",
62
65
  "lib/scout/indiferent_hash/options.rb",
63
66
  "lib/scout/indiferent_hash/serialize.rb",
64
67
  "lib/scout/log.rb",
@@ -113,26 +116,38 @@ Gem::Specification.new do |s|
113
116
  "lib/scout/simple_opt/get.rb",
114
117
  "lib/scout/simple_opt/parse.rb",
115
118
  "lib/scout/simple_opt/setup.rb",
119
+ "lib/scout/testing.rb",
120
+ "lib/scout/testing/availability.rb",
121
+ "lib/scout/testing/scout_state.rb",
116
122
  "lib/scout/tmpfile.rb",
117
123
  "research/annotations-data-analysis.md",
118
124
  "research/behavior-probes.md",
119
125
  "research/commands-streaming-analysis.md",
120
126
  "research/design-philosophy-analysis.md",
121
127
  "research/doc-audit-findings.md",
128
+ "research/doc-structure.md",
122
129
  "research/ecosystem-attribution.md",
123
130
  "research/implementation-inventory-core.md",
124
131
  "research/implementation-inventory-open.md",
125
132
  "research/implementation-inventory-path-persist-resource.md",
126
133
  "research/io-paths-analysis.md",
134
+ "research/lock-namespace-blindness.md",
127
135
  "research/persistence-resources-analysis.md",
136
+ "research/probing-guide.md",
128
137
  "research/synthesis-report.md",
138
+ "research/testing-availability-scout-ai-handoff.md",
139
+ "research/testing-availability-scout-rig-handoff.md",
129
140
  "scout-essentials.gemspec",
130
141
  "share/color/color_names",
131
142
  "share/color/diverging_colors.hex",
132
143
  "share/software/install_helpers",
133
144
  "test/scout/annotation/test_array.rb",
145
+ "test/scout/annotation/test_dup_clone.rb",
134
146
  "test/scout/indiferent_hash/test_case_insensitive.rb",
147
+ "test/scout/indiferent_hash/test_case_insensitive_staleness.rb",
148
+ "test/scout/indiferent_hash/test_json.rb",
135
149
  "test/scout/indiferent_hash/test_options.rb",
150
+ "test/scout/indiferent_hash/test_string2hash_false.rb",
136
151
  "test/scout/log/test_color.rb",
137
152
  "test/scout/log/test_fingerprint.rb",
138
153
  "test/scout/log/test_progress.rb",
@@ -144,6 +159,9 @@ Gem::Specification.new do |s|
144
159
  "test/scout/misc/test_matching.rb",
145
160
  "test/scout/misc/test_math.rb",
146
161
  "test/scout/misc/test_system.rb",
162
+ "test/scout/misc/test_timespan.rb",
163
+ "test/scout/open/test_compression_detection.rb",
164
+ "test/scout/open/test_forced_gzip_broken.rb",
147
165
  "test/scout/open/test_lock.rb",
148
166
  "test/scout/open/test_remote.rb",
149
167
  "test/scout/open/test_stream.rb",
@@ -172,16 +190,21 @@ Gem::Specification.new do |s|
172
190
  "test/scout/test_log.rb",
173
191
  "test/scout/test_misc.rb",
174
192
  "test/scout/test_named_array.rb",
193
+ "test/scout/test_named_array_resolution.rb",
175
194
  "test/scout/test_open.rb",
176
195
  "test/scout/test_path.rb",
177
196
  "test/scout/test_persist.rb",
178
197
  "test/scout/test_resource.rb",
198
+ "test/scout/test_testing.rb",
179
199
  "test/scout/test_tmpfile.rb",
200
+ "test/scout/test_tmpfile_cleanup.rb",
201
+ "test/scout/testing/test_availability.rb",
202
+ "test/scout/testing/test_state_bindings.rb",
180
203
  "test/test_helper.rb"
181
204
  ]
182
205
  s.homepage = "http://github.com/mikisvaz/scout-essentials".freeze
183
206
  s.licenses = ["MIT".freeze]
184
- s.rubygems_version = "3.7.2".freeze
207
+ s.rubygems_version = "4.0.23".freeze
185
208
  s.summary = "Scout essential tools".freeze
186
209
 
187
210
  s.specification_version = 4
@@ -0,0 +1,31 @@
1
+ require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
2
+ require 'scout/annotation'
3
+
4
+ class TestAnnotationDupClone < Test::Unit::TestCase
5
+ module AnnotSetupA
6
+ extend Annotation
7
+ annotation :organism, :tissue
8
+ end
9
+
10
+ def test_dup_strips_annotations_clone_keeps
11
+ a = AnnotSetupA.setup('S004', 'Human', 'Liver')
12
+ assert Annotation.is_annotated?(a)
13
+
14
+ d = a.dup
15
+ assert ! Annotation.is_annotated?(d)
16
+ assert_equal 'S004', d
17
+
18
+ c = a.clone
19
+ assert Annotation.is_annotated?(c)
20
+ assert_equal 'Human', c.organism
21
+ end
22
+
23
+ def test_setup_on_frozen_object_returns_new_annotated_copy
24
+ a = 'S007'.freeze
25
+ r = AnnotSetupA.setup(a, organism: 'Human')
26
+ assert ! Annotation.is_annotated?(a) # original untouched
27
+ assert a.frozen?
28
+ assert Annotation.is_annotated?(r) # result is a new, annotated copy
29
+ assert_equal 'Human', r.organism
30
+ end
31
+ end
@@ -0,0 +1,27 @@
1
+ require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
2
+ require 'scout/indiferent_hash/case_insensitive'
3
+
4
+ class TestCaseInsensitiveHashStaleness < Test::Unit::TestCase
5
+ def test_miss_then_write_leaves_key_invisible
6
+ ci = CaseInsensitiveHash.setup({'Key' => 1})
7
+ assert_nil ci['fourth'] # builds the downcase map without "Fourth"
8
+ ci['Fourth'] = 9 # plain Hash#[]= does not invalidate the map
9
+ assert_equal 9, ci['Fourth'] # exact lookup still works
10
+ assert_nil ci['fourth'] # but case-insensitive lookup is stale
11
+ end
12
+
13
+ def test_symbol_key_added_after_a_miss_is_invisible
14
+ ci = CaseInsensitiveHash.setup({'Key' => 1})
15
+ assert_nil ci['absent'] # miss freezes the downcase map
16
+ ci[:third] = 7 # added after the freeze
17
+ assert_equal 7, ci[:third] # symbol lookup is exact and works
18
+ assert_nil ci['third'] # string lookup is stale
19
+ end
20
+
21
+ def test_read_side_only
22
+ ci = CaseInsensitiveHash.setup({'Key' => 1})
23
+ ci['Other'] = 5
24
+ assert_equal 5, ci['other']
25
+ assert_equal 5, ci['OTHER']
26
+ end
27
+ end
@@ -0,0 +1,13 @@
1
+ require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
2
+ require File.expand_path(__FILE__).sub(%r(.*/test/), '').sub(/test_(.*)\.rb/,'\1')
3
+
4
+ class TestIndiferentHash < Test::Unit::TestCase
5
+ def test_json
6
+ h = IndiferentHash.setup({})
7
+ h['a'] = 1
8
+ h[:a] = 1
9
+
10
+ assert_equal ['a'], JSON.parse(h.to_json).keys
11
+ end
12
+ end
13
+
@@ -0,0 +1,13 @@
1
+ require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
2
+ require 'scout/indiferent_hash'
3
+
4
+ class TestIndiferentString2HashFalse < Test::Unit::TestCase
5
+ def test_true_becomes_trueclass
6
+ assert_equal({ 'a' => true }, IndiferentHash.string2hash('a=true'))
7
+ end
8
+
9
+ def test_false_stays_the_string_false
10
+ # asymmetry: 'true' converts, 'false' does not
11
+ assert_equal({ 'a' => 'false' }, IndiferentHash.string2hash('a=false'))
12
+ end
13
+ end
@@ -0,0 +1,30 @@
1
+ require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
2
+ require 'scout/misc/format'
3
+
4
+ class TestMiscTimespan < Test::Unit::TestCase
5
+ def test_plain_units
6
+ assert_equal 5, Misc.timespan('5s')
7
+ assert_equal 60, Misc.timespan('1m')
8
+ assert_equal 3600, Misc.timespan('1h')
9
+ assert_equal 86400, Misc.timespan('1d')
10
+ end
11
+
12
+ def test_negative
13
+ assert_equal(-5, Misc.timespan('-5s'))
14
+ end
15
+
16
+ def test_space_separated_units_add_up
17
+ assert_equal 90, Misc.timespan('1m 30s')
18
+ assert_equal 5400, Misc.timespan('1h 30m')
19
+ end
20
+
21
+ def test_glued_units_raise
22
+ # The scan is /(\d+)(\w*)/ and greedy: '1m30s' pairs 1 with the
23
+ # unknown token 'm30s', so the multiplication sees nil and raises.
24
+ assert_raise(TypeError) { Misc.timespan('1m30s') }
25
+ end
26
+
27
+ def test_unknown_unit_raises
28
+ assert_raise(TypeError) { Misc.timespan('1x') }
29
+ end
30
+ end
@@ -0,0 +1,32 @@
1
+ require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
2
+
3
+ class TestOpenCompressionDetection < Test::Unit::TestCase
4
+ def test_suffix_detection_is_lowercase_only
5
+ TmpFile.with_file('content') do |file|
6
+ `gzip #{file}`
7
+ gz = file + '.gz'
8
+ assert Open.gzip?(gz)
9
+ assert Open.compressed?(gz)
10
+
11
+ upper = gz + '.GZ.tmp'
12
+ FileUtils.mv gz, upper
13
+ FileUtils.cp upper, gz + '.2' if false
14
+ assert ! Open.gzip?(upper), '.GZ is not detected'
15
+ assert ! Open.compressed?(upper)
16
+ FileUtils.mv upper, gz
17
+ end
18
+ end
19
+
20
+ def test_gz_magic_bytes_without_suffix_stay_raw
21
+ content = "a\nb\n"
22
+ TmpFile.with_file(content) do |file|
23
+ `gzip #{file}`
24
+ gz = file + '.gz'
25
+ noext = gz + '.noext'
26
+ FileUtils.mv gz, noext
27
+ assert_equal [31, 139].first, Open.read(noext).bytes.first
28
+ assert_equal 'a', Open.read(noext, :noz => true).bytes.first.chr rescue nil
29
+ FileUtils.rm noext
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,25 @@
1
+ require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
2
+
3
+ class TestOpenForcedGzipBroken < Test::Unit::TestCase
4
+ # Regression test: :gzip => true must NOT be used to force decompression.
5
+ # The option is forwarded to CMD.cmd('gzip', ...) as a command-line flag,
6
+ # producing `gzip --gzip` which fails; :no_fail swallows the failure and
7
+ # the caller silently gets an empty string.
8
+ def test_forced_gzip_returns_empty_string
9
+ TmpFile.with_file("a\nb\n") do |file|
10
+ `gzip #{file}`
11
+ gz = file + '.gz'
12
+
13
+ # automatic detection works
14
+ assert_equal "a\nb\n", Open.read(gz)
15
+
16
+ # forcing with :gzip on the same file yields "" (documented behaviour)
17
+ assert_equal '', Open.read(gz, :gzip => true)
18
+
19
+ # and on a plain file too
20
+ TmpFile.with_file("plain\n") do |plain|
21
+ assert_equal '', Open.read(plain, :gzip => true)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,49 @@
1
+ require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
2
+ require 'scout/named_array'
3
+
4
+ class TestNamedArrayResolution < Test::Unit::TestCase
5
+ def test_exact_match_wins_over_parenthesized_field
6
+ names = ['id', 'Organism', 'Organism (Human)']
7
+ assert_equal 0, NamedArray.identify_name(names, 'id')
8
+ assert_equal 1, NamedArray.identify_name(names, 'Organism')
9
+ end
10
+
11
+ def test_paren_fuzzy_match
12
+ names = ['Organism (Human)']
13
+ assert_equal 0, NamedArray.identify_name(names, 'Human')
14
+ # and the other direction
15
+ names = ['Human']
16
+ assert_equal 0, NamedArray.identify_name(names, 'Organism (Human)')
17
+ end
18
+
19
+ def test_space_prefix_fuzzy_match
20
+ names = ['Sample id']
21
+ assert_equal 0, NamedArray.identify_name(names, 'Sample')
22
+ names = ['Sample']
23
+ assert_equal 0, NamedArray.identify_name(names, 'Sample id')
24
+ end
25
+
26
+ def test_fuzzy_match_is_case_sensitive
27
+ names = ['Alpha']
28
+ assert_nil NamedArray.identify_name(names, 'alpha')
29
+ end
30
+
31
+ def test_numeric_string_resolves_as_position
32
+ names = %w(a b c)
33
+ assert_equal 2, NamedArray.identify_name(names, '2')
34
+ end
35
+
36
+ def test_unknown_name_reads_nil_and_writes_noop
37
+ row = NamedArray.setup([1, 2, 3], [:a, :b, :c])
38
+ assert_nil row[:nope]
39
+ assigned = (row['nope'] = 4)
40
+ assert_equal 4, assigned # assignment expression returns value
41
+ assert_equal 3, row.length # but nothing was stored
42
+ assert_nil row.positions('nope')
43
+ end
44
+
45
+ def test_values_at_on_unknown_name_raises_type_error
46
+ row = NamedArray.setup([1, 2, 3], [:a, :b, :c])
47
+ assert_raise(TypeError) { row.values_at('nope') }
48
+ end
49
+ end
@@ -0,0 +1,195 @@
1
+ require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
2
+ require 'scout/testing'
3
+ require 'scout/testing/scout_state'
4
+
5
+ class TestScoutTesting < Test::Unit::TestCase
6
+ def test_tmpdir_is_unique_and_ensure_cleaned
7
+ paths = []
8
+ assert_raise(RuntimeError) do
9
+ Scout::Testing.with_tmpdir(parent: tmpdir) do |path|
10
+ paths << path
11
+ File.write(File.join(path, 'real-file'), 'fixture')
12
+ assert_equal 'fixture', File.read(File.join(path, 'real-file'))
13
+ raise 'expected'
14
+ end
15
+ end
16
+ refute File.exist?(paths.first)
17
+
18
+ first = Scout::Testing.create_tmpdir(parent: tmpdir)
19
+ second = Scout::Testing.create_tmpdir(parent: tmpdir)
20
+ refute_equal first, second
21
+ assert File.directory?(first)
22
+ assert File.directory?(second)
23
+ Scout::Testing.remove_tmpdir(first)
24
+ Scout::Testing.remove_tmpdir(second)
25
+ end
26
+
27
+ def test_fixture_paths_resolve_non_git_consumer_from_file_and_directory
28
+ Scout::Testing.with_tmpdir(parent: Dir.tmpdir) do |root|
29
+ test_directory = File.join(root, 'test', 'scout')
30
+ test_file = File.join(test_directory, 'test_consumer.rb')
31
+ fixture_file = File.join(root, 'test', 'fixtures', 'example.txt')
32
+ FileUtils.mkdir_p(test_directory)
33
+ FileUtils.mkdir_p(File.dirname(fixture_file))
34
+ File.write(test_file, '# synthetic non-Git consumer')
35
+ File.write(fixture_file, 'consumer fixture')
36
+ refute File.exist?(File.join(root, '.git'))
37
+
38
+ assert_equal root, Scout::Testing.repository_root(test_file)
39
+ assert_equal root, Scout::Testing.repository_root(test_directory)
40
+ assert_equal File.join(root, 'test/fixtures'),
41
+ Scout::Testing.fixture_root(from: test_file)
42
+ assert_equal fixture_file,
43
+ Scout::Testing.fixture_path('example.txt', from: test_file)
44
+ assert_equal 'consumer fixture',
45
+ File.read(Scout::Testing.fixture_path('example.txt', from: test_file))
46
+ end
47
+ end
48
+
49
+ def test_repository_root_git_and_non_git_directory_inputs
50
+ repository = File.expand_path('../..', __dir__)
51
+ assert_equal repository, Scout::Testing.repository_root(__dir__)
52
+ assert_equal repository, Scout::Testing.repository_root(__FILE__)
53
+
54
+ Scout::Testing.with_tmpdir(parent: Dir.tmpdir) do |root|
55
+ FileUtils.mkdir_p(File.join(root, 'lib', 'consumer'))
56
+ assert_equal root, Scout::Testing.repository_root(root)
57
+ assert_equal root, Scout::Testing.repository_root(File.join(root, 'lib'))
58
+ end
59
+ end
60
+
61
+ def test_path_assertions
62
+ assert Path === tmpdir
63
+ assert_equal_path '.', File.expand_path('.')
64
+ assert_path_exists __FILE__
65
+ end
66
+
67
+ def test_generic_require_is_standard_library_only_in_fresh_process
68
+ root = File.expand_path('../..', __dir__)
69
+ lib = File.join(root, 'lib')
70
+ script = <<~'RUBY'
71
+ $LOAD_PATH.unshift ARGV.fetch(1)
72
+ require 'scout/testing'
73
+ forbidden = $LOADED_FEATURES.grep(%r{/scout/(?:tmpfile|persist|open)(?:/|\.rb)})
74
+ abort "Scout components loaded: #{forbidden.join(', ')}" unless forbidden.empty?
75
+ abort 'TmpFile was initialized' if defined?(TmpFile)
76
+ abort 'Persist was initialized' if defined?(Persist)
77
+ puts 'generic-clean'
78
+ RUBY
79
+ output = IO.popen([RbConfig.ruby, '-e', script, root, lib], &:read)
80
+ assert $?.success?, output
81
+ assert_equal "generic-clean\n", output
82
+ end
83
+
84
+ def test_generic_source_has_no_scout_component_dependency
85
+ source = File.read(File.expand_path('../../lib/scout/testing.rb', __dir__))
86
+ refute_match(/require.*(?:tmpfile|persist|open)/, source)
87
+ refute_match(/\b(?:TmpFile|Persist|Open)\b/, source)
88
+ end
89
+
90
+ def test_state_layer_loads_components_explicitly
91
+ root = File.expand_path('../..', __dir__)
92
+ lib = File.join(root, 'lib')
93
+ script = <<~'RUBY'
94
+ $LOAD_PATH.unshift ARGV.fetch(1)
95
+ require 'scout/testing'
96
+ abort 'TmpFile loaded too early' if defined?(TmpFile)
97
+ require 'scout/testing/scout_state'
98
+ abort 'State API missing' unless Scout::Testing.const_defined?(:State) &&
99
+ Scout::Testing.respond_to?(:with_isolated_state)
100
+ abort 'Scout components missing' unless defined?(TmpFile) && defined?(Persist) && defined?(Open)
101
+ puts 'state-loaded'
102
+ RUBY
103
+ output = IO.popen([RbConfig.ruby, '-e', script, root, lib], &:read)
104
+ assert $?.success?, output
105
+ assert_equal "state-loaded\n", output
106
+ end
107
+
108
+ def test_state_restored_after_exception
109
+ Persist::MEMORY_CACHE[:original] = :keep
110
+ prior = state_snapshot
111
+ assert_raise(RuntimeError) do
112
+ Scout::Testing.with_isolated_state(File.join(tmpdir, 'isolated')) do
113
+ assert_equal File.join(tmpdir, 'isolated', 'tmpfiles'), TmpFile.tmpdir
114
+ assert_equal File.join(tmpdir, 'isolated', 'persistence-cache'), Persist.cache_dir.find
115
+ assert_equal File.join(tmpdir, 'isolated', 'persistence-locks'), Persist.lock_dir
116
+ assert_equal File.join(tmpdir, 'isolated', 'remote-cache'), Open.remote_cache_dir
117
+ assert_equal File.join(tmpdir, 'isolated', 'sensible-write'), Open.sensible_write_dir
118
+ assert_equal File.join(tmpdir, 'isolated', 'sensible-write-locks'), Open.sensible_write_lock_dir
119
+ assert_empty Persist::MEMORY_CACHE
120
+ Persist::MEMORY_CACHE[:temporary] = :discard
121
+ raise 'expected'
122
+ end
123
+ end
124
+ assert_state_snapshot prior
125
+ end
126
+
127
+ def test_state_restores_unset_and_set_to_nil_instance_variables
128
+ TmpFile.remove_instance_variable(:@tmpdir) if TmpFile.instance_variable_defined?(:@tmpdir)
129
+ Persist.instance_variable_set(:@lock_dir, nil)
130
+ Open.instance_variable_set(:@sensible_write_dir, nil)
131
+
132
+ Scout::Testing.with_isolated_state(File.join(tmpdir, 'isolated-nil')) { }
133
+
134
+ refute TmpFile.instance_variable_defined?(:@tmpdir)
135
+ assert Persist.instance_variable_defined?(:@lock_dir)
136
+ assert_nil Persist.instance_variable_get(:@lock_dir)
137
+ assert Open.instance_variable_defined?(:@sensible_write_dir)
138
+ assert_nil Open.instance_variable_get(:@sensible_write_dir)
139
+ end
140
+
141
+ def test_state_restored_when_activation_raises
142
+ prior = state_snapshot
143
+ singleton = Persist.singleton_class
144
+ singleton.alias_method :cache_dir_set_for_testing, :cache_dir=
145
+ singleton.define_method(:cache_dir=) { |_value| raise 'activation failed' }
146
+
147
+ assert_raise(RuntimeError) do
148
+ Scout::Testing.with_isolated_state(File.join(tmpdir, 'activation-failure')) { flunk 'block should not run' }
149
+ end
150
+ assert_state_snapshot prior
151
+ ensure
152
+ if singleton && singleton.method_defined?(:cache_dir_set_for_testing)
153
+ singleton.alias_method :cache_dir=, :cache_dir_set_for_testing
154
+ singleton.remove_method :cache_dir_set_for_testing
155
+ end
156
+ end
157
+
158
+ def test_runtime_load_does_not_activate_testing
159
+ root = File.expand_path('../..', __dir__)
160
+ lib = File.join(root, 'lib')
161
+ script = <<~'RUBY'
162
+ $LOAD_PATH.unshift ARGV.fetch(1)
163
+ require 'scout-essentials'
164
+ abort 'testing entrypoint loaded' if $LOADED_FEATURES.any? { |f| f.end_with?('/scout/testing.rb') }
165
+ abort 'testing module defined' if defined?(Scout::Testing)
166
+ puts 'runtime-clean'
167
+ RUBY
168
+ output = IO.popen([RbConfig.ruby, '-e', script, root, lib], &:read)
169
+ assert $?.success?, output
170
+ assert_equal "runtime-clean\n", output
171
+ end
172
+
173
+ private
174
+
175
+ def state_snapshot
176
+ [[TmpFile, :@tmpdir], [Persist, :@cache_dir], [Persist, :@lock_dir],
177
+ [Open, :@remote_cache_dir], [Open, :@sensible_write_dir],
178
+ [Open, :@sensible_write_lock_dir]].to_h do |owner, ivar|
179
+ [[owner, ivar], [owner.instance_variable_defined?(ivar), owner.instance_variable_get(ivar)]]
180
+ end.merge(memory_cache: Persist::MEMORY_CACHE.dup)
181
+ end
182
+
183
+ def assert_state_snapshot(snapshot)
184
+ snapshot.each do |key, value|
185
+ if key == :memory_cache
186
+ assert_equal value, Persist::MEMORY_CACHE
187
+ else
188
+ owner, ivar = key
189
+ existed, saved_value = value
190
+ assert_equal existed, owner.instance_variable_defined?(ivar), "#{owner} #{ivar} definedness"
191
+ assert_equal saved_value, owner.instance_variable_get(ivar) if existed
192
+ end
193
+ end
194
+ end
195
+ end
@@ -0,0 +1,23 @@
1
+ require File.expand_path(__FILE__).sub(%r(/test/.*), '/test/test_helper.rb')
2
+ require 'tmpdir'
3
+ require 'scout/tmpfile'
4
+
5
+ class TestTmpFileWithFileCleanup < Test::Unit::TestCase
6
+ def test_exception_leaves_temp_file_behind
7
+ # with_file has no ensure around the yield: a raising block leaves
8
+ # the temporary file in the tmpdir. Cleanup is the caller's job.
9
+ Dir.mktmpdir do |d|
10
+ TmpFile.tmpdir = d
11
+ assert_raise { TmpFile.with_file('data') { |f| raise 'boom' if File.exist?(f) } }
12
+ assert_equal 1, Dir.children(d).length, 'temp file left behind by raising block'
13
+ end
14
+ end
15
+
16
+ def test_normal_exit_removes_file
17
+ seen = nil
18
+ TmpFile.with_file('data') do |file|
19
+ seen = file
20
+ end
21
+ assert ! File.exist?(seen)
22
+ end
23
+ end