1
1
require "arduino_ci"
2
2
3
+ # This class is meant for the :around behavior of RSpec test cases so that
4
+ # we can make temporary directories in test cases. Note that since test cases
5
+ # are evaluated on load, any temp directories that were created will not exist
6
+ # by the time the test runs. So this class handles all of the particulars
7
+ # around creating a fake library directory on time and configuring the backend
8
+ # to properly use it.
3
9
class FakeLibDir
4
10
5
11
attr_reader :config_dir
@@ -18,16 +24,18 @@ def initialize
18
24
19
25
# designed to be called by rspec's "around" function
20
26
def in_pristine_fake_libraries_dir ( example )
21
- d = Dir . mktmpdir
22
- begin
27
+ # we will make a dummy directory to contain the libraries directory,
28
+ # and use that directory in a dummy config which we will pass to the backend.
29
+ # then we can run the test case
30
+ Dir . mktmpdir do |d |
23
31
# write a yaml file containing the current directory
24
32
dummy_config = { "directories" => { "user" => d . to_s } }
25
33
@arduino_dir = Pathname . new ( d )
26
34
@libraries_dir = @arduino_dir + "libraries"
27
35
Dir . mkdir ( @libraries_dir )
28
36
29
- f = File . open ( @config_file , "w" )
30
- begin
37
+ # with the config file, enforce a structure similar to a temp file -- delete after use
38
+ File . open ( @config_file , "w" ) do | f |
31
39
f . write dummy_config . to_yaml
32
40
f . close
33
41
example . run
@@ -39,15 +47,14 @@ def in_pristine_fake_libraries_dir(example)
39
47
end
40
48
end
41
49
ensure
50
+ # the tmp dir will be cleaned up automatically, but if we did our own symlink hack then here is the place to clean it up
42
51
if ArduinoCI ::Host . needs_symlink_hack?
43
52
stdout , stderr , exitstatus = Open3 . capture3 ( 'cmd.exe' , "/c rmdir /s /q #{ ArduinoCI ::Host . pathname_to_windows ( d ) } " )
44
53
unless exitstatus . success?
45
54
puts "====== rmdir of #{ d } failed"
46
55
puts stdout
47
56
puts stderr
48
57
end
49
- else
50
- FileUtils . remove_entry ( d )
51
58
end
52
59
end
53
60
end
0 commit comments