test_temp_file_helper 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +112 -0
- data/lib/test_temp_file_helper.rb +65 -0
- data/lib/test_temp_file_helper/rake.rb +89 -0
- data/lib/test_temp_file_helper/version.rb +19 -0
- metadata +106 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dc79e2ac38d87941a0a08e4edebbd1e92763eafc
|
4
|
+
data.tar.gz: b71bd5e044cc21144eac10859e200586573b1704
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0ff00fe7c5e03bc28b7aa04ed269ba07844ee558990ef8d5c39cf37c23546561fb92a7d5357f535fc3bcf1222bede028b33c2666a63143d192b4a4a1cb6ab422
|
7
|
+
data.tar.gz: e98c42a8b16eee67d180b650260743064b8d872b8ab3f7b575bc50e527218dce11ae28b3de78e3b40b41247a19152ceec0d2f7b9f44df4129300dc4316c9b342
|
data/README.md
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
## test_temp_file_helper Gem
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/test_temp_file_helper.svg)](https://badge.fury.io/rb/test_temp_file_helper)
|
4
|
+
[![Build Status](https://travis-ci.org/18F/test_temp_file_helper.svg?branch=master)](https://travis-ci.org/18F/test_temp_file_helper)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/18F/test_temp_file_helper/badges/gpa.svg)](https://codeclimate.com/github/18F/test_temp_file_helper)
|
6
|
+
[![Test Coverage](https://codeclimate.com/github/18F/test_temp_file_helper/badges/coverage.svg)](https://codeclimate.com/github/18F/test_temp_file_helper)
|
7
|
+
|
8
|
+
The `TestTempFileHelper::TempFileHelper` class manages the creation and
|
9
|
+
cleanup of temporary files in automated tests.
|
10
|
+
|
11
|
+
Downloads and API docs are available on the [test_temp_file_helper RubyGems
|
12
|
+
page](https://rubygems.org/gems/test_temp_file_helper). API documentation is
|
13
|
+
written using [YARD markup](http://yardoc.org/).
|
14
|
+
|
15
|
+
Contributed by the 18F team, part of the United States General Services
|
16
|
+
Administration: https://18f.gsa.gov/
|
17
|
+
|
18
|
+
### Motivation
|
19
|
+
|
20
|
+
Rather than reimplement [Pyfakefs](http://code.google.com/p/pyfakefs) in Ruby,
|
21
|
+
in the short-term, I wrote this class to emulate Google's `TEST_DATADIR` and
|
22
|
+
`TEST_TMPDIR` convention instead, so that I could test code that works with
|
23
|
+
the file system.
|
24
|
+
|
25
|
+
### Installation
|
26
|
+
|
27
|
+
Add this line to your application's Gemfile:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
gem 'test_temp_file_helper'
|
31
|
+
```
|
32
|
+
|
33
|
+
And then execute:
|
34
|
+
```
|
35
|
+
$ bundle
|
36
|
+
```
|
37
|
+
|
38
|
+
Or install it yourself as:
|
39
|
+
```
|
40
|
+
$ gem install test_temp_file_helper
|
41
|
+
```
|
42
|
+
|
43
|
+
### Usage
|
44
|
+
|
45
|
+
First, add the following to your `Rakefile`:
|
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
|
66
|
+
`TempFileHelper.teardown` in your test's `teardown` method.
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
require 'minitest/autorun'
|
70
|
+
require 'test_temp_file_helper'
|
71
|
+
|
72
|
+
class MyTest < ::Minitest::Test
|
73
|
+
def setup
|
74
|
+
@temp_file_helper = TestTempFileHelper::TempFileHelper.new
|
75
|
+
end
|
76
|
+
|
77
|
+
def teardown
|
78
|
+
@temp_file_helper.teardown
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_something_that_handles_files
|
82
|
+
dir_path = @temp_file_helper.mkdir(File.join('path', 'to', 'dir'))
|
83
|
+
file_path = @temp_file_helper.mkfile(File.join('path', 'to', 'file'))
|
84
|
+
...
|
85
|
+
end
|
86
|
+
end
|
87
|
+
```
|
88
|
+
|
89
|
+
### Contributing
|
90
|
+
|
91
|
+
1. Fork the repo ( https://github.com/18F/test_temp_file_helper/fork )
|
92
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
93
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
94
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
95
|
+
5. Create a new Pull Request
|
96
|
+
|
97
|
+
Feel free to ping [@mbland](https://github.com/mbland) with any questions you
|
98
|
+
may have, especially if the current documentation should've addressed your
|
99
|
+
needs, but didn't.
|
100
|
+
|
101
|
+
### Public domain
|
102
|
+
|
103
|
+
This project is in the worldwide [public domain](LICENSE.md). As stated in
|
104
|
+
[CONTRIBUTING](CONTRIBUTING.md):
|
105
|
+
|
106
|
+
> This project is in the public domain within the United States, and copyright
|
107
|
+
> and related rights in the work worldwide are waived through the
|
108
|
+
> [CC0 1.0 Universal public domain dedication](https://creativecommons.org/publicdomain/zero/1.0/).
|
109
|
+
>
|
110
|
+
> All contributions to this project will be released under the CC0 dedication.
|
111
|
+
> By submitting a pull request, you are agreeing to comply with this waiver of
|
112
|
+
> copyright interest.
|
@@ -0,0 +1,65 @@
|
|
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 "test_temp_file_helper/version"
|
18
|
+
|
19
|
+
module TestTempFileHelper
|
20
|
+
# Automatically generates and cleans up temporary files in automated tests.
|
21
|
+
# Performs its operations in the directory specified by the +TEST_TMPDIR+
|
22
|
+
# environment variable.
|
23
|
+
class TempFileHelper
|
24
|
+
# @param tmpdir [String] (optional) if specified, overrides +TEST_TMPDIR+
|
25
|
+
def initialize(tmpdir: nil)
|
26
|
+
@tmpdir = tmpdir || ENV['TEST_TMPDIR']
|
27
|
+
@files = []
|
28
|
+
@dirs = []
|
29
|
+
end
|
30
|
+
|
31
|
+
# Creates a temporary test directory relative to TEST_TMPDIR.
|
32
|
+
# @param relative_path [String] directory to create
|
33
|
+
# @return [String] File.join(@tmpdir, relative_path)
|
34
|
+
def mkdir(relative_path)
|
35
|
+
components = relative_path.split(File::SEPARATOR)
|
36
|
+
components = components.delete_if {|i| i == '.'}
|
37
|
+
current = @tmpdir
|
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
|
44
|
+
end
|
45
|
+
|
46
|
+
# Creates a temporary file relative to TEST_TMPDIR.
|
47
|
+
# @param relative_path [String] file to create
|
48
|
+
# @param content [String] (optional) content to include in the file
|
49
|
+
# @return [String] File.join(@tmpdir, relative_path)
|
50
|
+
def mkfile(relative_path, content: '')
|
51
|
+
mkdir File.dirname(relative_path)
|
52
|
+
filename = File.join(@tmpdir, relative_path)
|
53
|
+
File.open(filename, 'w') {|f| f << content}
|
54
|
+
@files << filename
|
55
|
+
filename
|
56
|
+
end
|
57
|
+
|
58
|
+
# Removes all files and directories created by the instance. Should be
|
59
|
+
# called from the test's +teardown+ method.
|
60
|
+
def teardown
|
61
|
+
@files.sort.uniq.each {|f| File.unlink f}
|
62
|
+
@dirs.sort.uniq.reverse.each {|d| Dir.rmdir d}
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,89 @@
|
|
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
|
@@ -0,0 +1,19 @@
|
|
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
|
+
module TestTempFileHelper
|
18
|
+
VERSION = "0.0.1"
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: test_temp_file_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Bland
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '10.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '10.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: codeclimate-test-reporter
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: The TestTempFileHelper::TempFileHelper class manages the creation and
|
70
|
+
cleanup of temporary files in automated tests.
|
71
|
+
email:
|
72
|
+
- michael.bland@gsa.gov
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- README.md
|
78
|
+
- lib/test_temp_file_helper.rb
|
79
|
+
- lib/test_temp_file_helper/rake.rb
|
80
|
+
- lib/test_temp_file_helper/version.rb
|
81
|
+
homepage: https://github.com/18F/test_temp_file_helper
|
82
|
+
licenses:
|
83
|
+
- CC0
|
84
|
+
metadata: {}
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 2.2.2
|
102
|
+
signing_key:
|
103
|
+
specification_version: 4
|
104
|
+
summary: Class for managing temporary files in automated tests
|
105
|
+
test_files: []
|
106
|
+
has_rdoc:
|