test_temp_file_helper 0.0.1 → 0.0.2
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 +4 -4
- data/README.md +11 -21
- data/lib/test_temp_file_helper.rb +15 -19
- data/lib/test_temp_file_helper/version.rb +1 -1
- metadata +2 -3
- data/lib/test_temp_file_helper/rake.rb +0 -89
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d4ff9b1bf75dba193364a17c27dff7b374cd4461
|
4
|
+
data.tar.gz: e6e3c1a973382bcc7f9cb6deeb73031a2bebc89e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f12e13b1e4ba1c1dab03db9ce81374ed8d8b778f49e7edb03c9b50b1b79a0cd2650d8c27d94c6dfc9fd32fd9af9cfeda43b448bdedcb3599ba21032c7d28fc97
|
7
|
+
data.tar.gz: 1530ea138872e299e88d254842442cb25808a66423dbc7e729efb1ebdb49cc48d8466b5a6f4c4450fcf809107c2c1157100b0a090701f26856debf94c510f83c
|
data/README.md
CHANGED
@@ -42,27 +42,7 @@ $ gem install test_temp_file_helper
|
|
42
42
|
|
43
43
|
### Usage
|
44
44
|
|
45
|
-
|
46
|
-
|
47
|
-
```ruby
|
48
|
-
require 'test_temp_file_helper/rake'
|
49
|
-
|
50
|
-
TestTempFileHelper::SetupTestEnvironmentTask.new do |t|
|
51
|
-
t.base_dir = File.dirname __FILE__
|
52
|
-
t.data_dir = File.join('test', 'data')
|
53
|
-
t.tmp_dir = File.join('test', 'tmp')
|
54
|
-
end
|
55
|
-
```
|
56
|
-
The `SetupTestEnvironmentTask` properties:
|
57
|
-
- `base_dir`: parent directory used to set the environment variables
|
58
|
-
- `data_dir`: directory relative to `base_dir` used to set `TEST_DATADIR`
|
59
|
-
- `tmp_dir`: directory relative to `base_dir` used to set `TEST_TMPDIR`
|
60
|
-
|
61
|
-
If an environment variable is already set, or if the corresponding property is
|
62
|
-
not set on the `SetupTestEnvironmentTask` object, the environment variable
|
63
|
-
will not be set or updated.
|
64
|
-
|
65
|
-
Then, create a `TempFileHelper` in your test's `setup` method, and call
|
45
|
+
Create a `TempFileHelper` in your test's `setup` method, and call
|
66
46
|
`TempFileHelper.teardown` in your test's `teardown` method.
|
67
47
|
|
68
48
|
```ruby
|
@@ -86,6 +66,16 @@ class MyTest < ::Minitest::Test
|
|
86
66
|
end
|
87
67
|
```
|
88
68
|
|
69
|
+
The temporary directory containing all generated files and directories is
|
70
|
+
set by the first of these items which is not `nil`:
|
71
|
+
|
72
|
+
1. the `tmp_dir` argument to `TempFileHelper.new`
|
73
|
+
2. the `TEST_TMPDIR` environment variable
|
74
|
+
3. `Dir.mktmpdir`
|
75
|
+
|
76
|
+
The path to the temporary directory itself can be accessed via
|
77
|
+
`TempFileHelper.tmpdir`.
|
78
|
+
|
89
79
|
### Contributing
|
90
80
|
|
91
81
|
1. Fork the repo ( https://github.com/18F/test_temp_file_helper/fork )
|
@@ -18,29 +18,27 @@ require "test_temp_file_helper/version"
|
|
18
18
|
|
19
19
|
module TestTempFileHelper
|
20
20
|
# Automatically generates and cleans up temporary files in automated tests.
|
21
|
-
#
|
22
|
-
#
|
21
|
+
#
|
22
|
+
# The temporary directory containing all generated files and directories is
|
23
|
+
# set by the first of these items which is not +nil+:
|
24
|
+
# - the +tmp_dir+ argument to +TempFileHelper.new+
|
25
|
+
# - the +TEST_TMPDIR+ environment variable
|
26
|
+
# - +Dir.mktmpdir+
|
23
27
|
class TempFileHelper
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
@
|
28
|
+
attr_accessor :tmpdir
|
29
|
+
|
30
|
+
# @param tmpdir [String] (optional) if set, determines the temp dir
|
31
|
+
def initialize(tmp_dir: nil)
|
32
|
+
@tmpdir = tmp_dir || ENV['TEST_TMPDIR'] || Dir.mktmpdir
|
29
33
|
end
|
30
34
|
|
31
35
|
# Creates a temporary test directory relative to TEST_TMPDIR.
|
32
36
|
# @param relative_path [String] directory to create
|
33
37
|
# @return [String] File.join(@tmpdir, relative_path)
|
34
38
|
def mkdir(relative_path)
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
until components.empty?
|
39
|
-
current = File.join current, components.shift
|
40
|
-
Dir.mkdir current unless File.exists? current
|
41
|
-
@dirs << current
|
42
|
-
end
|
43
|
-
@dirs.last
|
39
|
+
new_dir = File.join @tmpdir, relative_path
|
40
|
+
FileUtils.mkdir_p new_dir
|
41
|
+
new_dir
|
44
42
|
end
|
45
43
|
|
46
44
|
# Creates a temporary file relative to TEST_TMPDIR.
|
@@ -51,15 +49,13 @@ module TestTempFileHelper
|
|
51
49
|
mkdir File.dirname(relative_path)
|
52
50
|
filename = File.join(@tmpdir, relative_path)
|
53
51
|
File.open(filename, 'w') {|f| f << content}
|
54
|
-
@files << filename
|
55
52
|
filename
|
56
53
|
end
|
57
54
|
|
58
55
|
# Removes all files and directories created by the instance. Should be
|
59
56
|
# called from the test's +teardown+ method.
|
60
57
|
def teardown
|
61
|
-
|
62
|
-
@dirs.sort.uniq.reverse.each {|d| Dir.rmdir d}
|
58
|
+
FileUtils.remove_entry @tmpdir
|
63
59
|
end
|
64
60
|
end
|
65
61
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: test_temp_file_helper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Bland
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -76,7 +76,6 @@ extra_rdoc_files: []
|
|
76
76
|
files:
|
77
77
|
- README.md
|
78
78
|
- lib/test_temp_file_helper.rb
|
79
|
-
- lib/test_temp_file_helper/rake.rb
|
80
79
|
- lib/test_temp_file_helper/version.rb
|
81
80
|
homepage: https://github.com/18F/test_temp_file_helper
|
82
81
|
licenses:
|
@@ -1,89 +0,0 @@
|
|
1
|
-
# test_temp_file_helper - Generates and cleans up temp files for automated tests
|
2
|
-
#
|
3
|
-
# Written in 2015 by Mike Bland (michael.bland@gsa.gov)
|
4
|
-
# on behalf of the 18F team, part of the US General Services Administration:
|
5
|
-
# https://18f.gsa.gov/
|
6
|
-
#
|
7
|
-
# To the extent possible under law, the author(s) have dedicated all copyright
|
8
|
-
# and related and neighboring rights to this software to the public domain
|
9
|
-
# worldwide. This software is distributed without any warranty.
|
10
|
-
#
|
11
|
-
# You should have received a copy of the CC0 Public Domain Dedication along
|
12
|
-
# with this software. If not, see
|
13
|
-
# <https://creativecommons.org/publicdomain/zero/1.0/>.
|
14
|
-
#
|
15
|
-
# @author Mike Bland (michael.bland@gsa.gov)
|
16
|
-
|
17
|
-
require 'rake/tasklib'
|
18
|
-
|
19
|
-
module TestTempFileHelper
|
20
|
-
|
21
|
-
# A Rake task that sets up the +TEST_DATADIR+ and +TEST_TMPDIR+ environment
|
22
|
-
# variables.
|
23
|
-
#
|
24
|
-
# Properties:
|
25
|
-
# - +base_dir+: parent directory used to set the environment variables
|
26
|
-
# - +data_dir+: directory relative to +base_dir+ used to set +TEST_DATADIR+
|
27
|
-
# - +tmp_dir+: directory relative to +base_dir+ used to set +TEST_TMPDIR+
|
28
|
-
#
|
29
|
-
# If an environment variable is already set, or if the corresponding
|
30
|
-
# property is not set on the +SetupTestEnvironmentTask+ object, the
|
31
|
-
# environment variable will not be set or updated.
|
32
|
-
#
|
33
|
-
# Usage (inside a +Rakefile+):
|
34
|
-
#
|
35
|
-
# require 'test_temp_file_helper/rake'
|
36
|
-
#
|
37
|
-
# TestTempFileHelper::SetupTestEnvironmentTask.new do |t|
|
38
|
-
# t.base_dir = File.dirname __FILE__
|
39
|
-
# t.data_dir = File.join('test', 'data')
|
40
|
-
# t.tmp_dir = File.join('test', 'tmp')
|
41
|
-
# end
|
42
|
-
class SetupTestEnvironmentTask < ::Rake::TaskLib
|
43
|
-
def initialize(name='test_temp_file_helper_setup_test_environment')
|
44
|
-
@name = name
|
45
|
-
@base_dir = nil
|
46
|
-
yield self if block_given?
|
47
|
-
|
48
|
-
set_environment_variable 'TEST_DATADIR', @data_dir
|
49
|
-
set_environment_variable 'TEST_TMPDIR', @tmp_dir
|
50
|
-
test_tmpdir = ENV['TEST_TMPDIR']
|
51
|
-
|
52
|
-
if test_tmpdir
|
53
|
-
rm_rf test_tmpdir if File.exists? test_tmpdir
|
54
|
-
directory test_tmpdir
|
55
|
-
task test: test_tmpdir
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
# Sets +ENV[var_name]+ as the concatenation of +base_dir+ and
|
60
|
-
# +relative_dir+.
|
61
|
-
# @param var_name [String]
|
62
|
-
# @param relative_dir [String]
|
63
|
-
def set_environment_variable(var_name, relative_dir)
|
64
|
-
unless ENV[var_name]
|
65
|
-
ENV[var_name] = File.join @base_dir, relative_dir if relative_dir
|
66
|
-
end
|
67
|
-
end
|
68
|
-
private :set_environment_variable
|
69
|
-
|
70
|
-
# Sets the base directory that serves as the common parent for the
|
71
|
-
# +TEST_DATADIR+ and +TEST_TMPDIR+ environment variables.
|
72
|
-
# @param dirname [String] parent dir of +data_dir+ and +tmp_dir+
|
73
|
-
def base_dir=(dirname)
|
74
|
-
@base_dir = dirname
|
75
|
-
end
|
76
|
-
|
77
|
-
# Sets +TEST_DATADIR+ using a path relative to the +base_dir+.
|
78
|
-
# @param dirname [String] path relative to +base_dir+
|
79
|
-
def data_dir=(dirname)
|
80
|
-
@data_dir = dirname
|
81
|
-
end
|
82
|
-
|
83
|
-
# Sets +TEST_TMPDIR+ using a path relative to the +base_dir+.
|
84
|
-
# @param dirname [String] path relative to +base_dir+
|
85
|
-
def tmp_dir=(dirname)
|
86
|
-
@tmp_dir = dirname
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|