temporary_directory 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@godobject.net>, 2016
4
+
5
+ This file is part of TemporaryDirectory.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ =end
19
+
20
+ Then(/^no temporary directories remain$/) do
21
+ expect(subsequent_temporary_directory_root_directory).to have(0).children
22
+ end
23
+
24
+ Then(/^one temporary directory remains$/) do
25
+ expect(subsequent_temporary_directory_root_directory).to have(1).children
26
+ end
27
+
28
+ Then(/^(\d+) temporary directories remain$/) do |amount_of_remaining_directories|
29
+ amount_of_remaining_directories = Integer(amount_of_remaining_directories)
30
+
31
+ expect(subsequent_temporary_directory_root_directory).to have(amount_of_remaining_directories).children
32
+ end
33
+
34
+ Then(/^the remaining temporary directories start with "([^"]*)"$/) do |prefix|
35
+ expect(subsequent_temporary_directory_root_directory).to have_at_least(1).children
36
+
37
+ remaining_file_names = @subsequent_temporary_directory_root_directory
38
+ .children
39
+ .map(&:basename)
40
+ .map(&:to_s)
41
+
42
+ expect(remaining_file_names).to all(start_with(prefix))
43
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@godobject.net>, 2016
4
+
5
+ This file is part of TemporaryDirectory.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ =end
19
+
20
+ require 'temporary_directory'
21
+ require 'rspec/collection_matchers'
22
+
23
+ # For compatibility with Ruby 2.1.x
24
+ if RUBY_VERSION.start_with?('2.1')
25
+ class Pathname
26
+ alias / +
27
+ end
28
+ end
29
+
30
+ World(GodObject::TemporaryDirectory::Helper.new(name_prefix: 'temporary_directory_cucumber'))
31
+
32
+ After do
33
+ ensure_absence_of_temporary_directory
34
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@godobject.net>, 2016
4
+
5
+ This file is part of TemporaryDirectory.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ =end
19
+
20
+ module Project
21
+ def project_root_directory
22
+ @project_root_directory ||= Pathname.new(__dir__) / '..' / '..'
23
+ end
24
+ end
25
+
26
+ World(Project)
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@godobject.net>, 2016
4
+
5
+ This file is part of TemporaryDirectory.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ =end
19
+
20
+ require 'English'
21
+
22
+ module SeparateEnvironmentExecution
23
+ attr_reader :output
24
+
25
+ def execute_in_separate_environment(command)
26
+ Bundler.with_clean_env do
27
+ Dir.chdir(temporary_directory) do
28
+ @output = `#{command}`
29
+ end
30
+ end
31
+ end
32
+
33
+ def create_gemfile(include_gems: [])
34
+ gemfile = temporary_directory / 'Gemfile'
35
+
36
+ gemfile.open('w') do |io|
37
+ io.puts(%(gem 'temporary_directory', path: '#{project_root_directory}'))
38
+
39
+ include_gems.each do |gem_name|
40
+ io.puts(%(gem '#{gem_name}', '= #{Gem.loaded_specs[gem_name].version}'))
41
+ end
42
+ end
43
+
44
+ gemfile
45
+ end
46
+ end
47
+
48
+ World(SeparateEnvironmentExecution)
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@godobject.net>, 2016
4
+
5
+ This file is part of TemporaryDirectory.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ =end
19
+
20
+ module SubsequentTemporaryDirectoryRootDirectory
21
+ attr_reader :subsequent_temporary_directory_root_directory
22
+
23
+ def create_root_for_subsequent_temporary_directories
24
+ @subsequent_temporary_directory_root_directory = temporary_directory / 'subsequent_temporary_directories'
25
+ @subsequent_temporary_directory_root_directory.mkpath
26
+ @subsequent_temporary_directory_root_directory
27
+ end
28
+ end
29
+
30
+ World(SubsequentTemporaryDirectoryRootDirectory)
@@ -0,0 +1,80 @@
1
+ Feature: Temporary directories for Cucumber
2
+
3
+ Background:
4
+ Given a file called "features/temporary_directory.feature" with the following content:
5
+ """
6
+ Feature: Something
7
+
8
+ Scenario: Something specific
9
+ Given some precondition
10
+ When the temporary directory is referenced
11
+ Then the state is somehow
12
+
13
+ Scenario: Another thing
14
+ Given some precondition
15
+ When something else happens
16
+ Then the state is somehow
17
+ """
18
+ And a file called "features/step_definitions/temporary_directory.rb" with the following content:
19
+ """
20
+ Given(/^some precondition$/) {}
21
+ When(/^something else happens$/) {}
22
+
23
+ When(/^the temporary directory is referenced$/) do
24
+ temporary_directory
25
+ end
26
+
27
+ Then(/^the state is somehow$/) {}
28
+ """
29
+
30
+ Scenario: Create a directory for each example
31
+ Given a file called "features/support/env.rb" with the following content:
32
+ """
33
+ require 'temporary_directory'
34
+
35
+ World(GodObject::TemporaryDirectory::Helper)
36
+
37
+ Before do
38
+ ensure_presence_of_temporary_directory
39
+ end
40
+ """
41
+ When I run "cucumber"
42
+ Then all scenarios and their steps should pass
43
+ And 2 temporary directories remain
44
+
45
+ Scenario: Create directories just on demand
46
+ Given a file called "features/support/env.rb" with the following content:
47
+ """
48
+ require 'temporary_directory'
49
+
50
+ World(GodObject::TemporaryDirectory::Helper)
51
+ """
52
+ When I run "cucumber"
53
+ Then all scenarios and their steps should pass
54
+ And one temporary directory remains
55
+
56
+ Scenario: Create directories with specific name prefixes
57
+ Given a file called "features/support/env.rb" with the following content:
58
+ """
59
+ require 'temporary_directory'
60
+
61
+ World(GodObject::TemporaryDirectory::Helper.new(name_prefix: 'marker'))
62
+ """
63
+ When I run "cucumber"
64
+ Then all scenarios and their steps should pass
65
+ And the remaining temporary directories start with "marker"
66
+
67
+ Scenario: Delete directories afterwards
68
+ Given a file called "features/support/env.rb" with the following content:
69
+ """
70
+ require 'temporary_directory'
71
+
72
+ World(GodObject::TemporaryDirectory::Helper)
73
+
74
+ After do
75
+ ensure_absence_of_temporary_directory
76
+ end
77
+ """
78
+ When I run "cucumber"
79
+ Then all scenarios and their steps should pass
80
+ And no temporary directories remain
@@ -0,0 +1,92 @@
1
+ Feature: Temporary directories for RSpec examples
2
+
3
+ Scenario: Create a directory for each example
4
+ Given a file called "temporary_directory_spec.rb" with the following content:
5
+ """
6
+ require 'temporary_directory'
7
+
8
+ describe 'something' do
9
+ include GodObject::TemporaryDirectory::Helper
10
+
11
+ before(:example) { ensure_presence_of_temporary_directory }
12
+
13
+ specify { expect(123).to be_a Numeric }
14
+ specify { expect('abc').to be_a String }
15
+ specify { expect(temporary_directory).to exist }
16
+ end
17
+ """
18
+ When I run "rspec temporary_directory_spec.rb"
19
+ Then all examples should pass
20
+ And 3 temporary directories remain
21
+
22
+ Scenario: Create directories just on demand
23
+ Given a file called "temporary_directory_spec.rb" with the following content:
24
+ """
25
+ require 'temporary_directory'
26
+
27
+ describe 'something' do
28
+ include GodObject::TemporaryDirectory::Helper
29
+
30
+ specify { expect(123).to be_a Numeric }
31
+ specify { expect('abc').to be_a String }
32
+ specify do
33
+ expect(temporary_directory).to exist
34
+ expect(temporary_directory).to be_a_directory
35
+ end
36
+ end
37
+ """
38
+ When I run "rspec temporary_directory_spec.rb"
39
+ Then all examples should pass
40
+ And one temporary directory remains
41
+
42
+ Scenario: Create directories with specific name prefixes
43
+ Given a file called "temporary_directory_spec.rb" with the following content:
44
+ """
45
+ require 'temporary_directory'
46
+
47
+ describe 'something' do
48
+ include GodObject::TemporaryDirectory::Helper.new(name_prefix: 'marker')
49
+
50
+ before(:example) { ensure_presence_of_temporary_directory }
51
+
52
+ specify { expect(123).to be_a Numeric }
53
+ end
54
+ """
55
+ When I run "rspec temporary_directory_spec.rb"
56
+ Then all examples should pass
57
+ And the remaining temporary directories start with "marker"
58
+
59
+ Scenario: Delete directories afterwards
60
+ Given a file called "temporary_directory_spec.rb" with the following content:
61
+ """
62
+ require 'temporary_directory'
63
+
64
+ describe 'something' do
65
+ include GodObject::TemporaryDirectory::Helper
66
+
67
+ after(:example) { ensure_absence_of_temporary_directory }
68
+
69
+ specify { expect(123).to be_a Numeric }
70
+ specify { expect('abc').to be_a String }
71
+
72
+ describe 'temporary directory' do
73
+ subject { temporary_directory }
74
+
75
+ it { is_expected.to exist }
76
+
77
+ describe 'sub-directory' do
78
+ let(:sub_directory) do
79
+ directory = temporary_directory + 'sub'
80
+ directory.mkpath
81
+ directory
82
+ end
83
+
84
+ specify { expect(sub_directory).to exist }
85
+ end
86
+ end
87
+ end
88
+ """
89
+ When I run "rspec temporary_directory_spec.rb"
90
+ Then all examples should pass
91
+ And no temporary directories remain
92
+
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@godobject.net>, 2016
4
+
5
+ This file is part of TemporaryDirectory.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ =end
19
+
20
+ require 'tmpdir'
21
+ require 'pathname'
22
+
23
+ module GodObject
24
+ module TemporaryDirectory
25
+
26
+ end
27
+ end
28
+
29
+ require_relative 'temporary_directory/service'
30
+ require_relative 'temporary_directory/helper'
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+ =begin
3
+ Copyright Alexander E. Fischer <aef@godobject.net>, 2016
4
+
5
+ This file is part of TemporaryDirectory.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted, provided that the above
9
+ copyright notice and this permission notice appear in all copies.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ =end
19
+
20
+ module GodObject
21
+ module TemporaryDirectory
22
+
23
+ module Helper
24
+ def self.new(name_prefix: nil,
25
+ base_directory: nil,
26
+ temporary_directory_service: Service.new(name_prefix: name_prefix, base_directory: base_directory))
27
+
28
+ mixin = Module.new do
29
+ include Helper
30
+ end
31
+
32
+ mixin.send(:define_method, :temporary_directory_service) do
33
+ temporary_directory_service
34
+ end
35
+
36
+ mixin
37
+ end
38
+
39
+ def temporary_directory
40
+ ensure_presence_of_temporary_directory
41
+ end
42
+
43
+ def ensure_presence_of_temporary_directory
44
+ @temporary_directory ||= create_temporary_directory!
45
+ end
46
+
47
+ def ensure_absence_of_temporary_directory
48
+ @temporary_directory.rmtree if @temporary_directory
49
+
50
+ nil
51
+ end
52
+
53
+ private
54
+
55
+ def create_temporary_directory!
56
+ temporary_directory_service.new
57
+ end
58
+
59
+ def temporary_directory_service
60
+ @temporary_directory_service ||= Service.new
61
+ end
62
+ end
63
+
64
+ end
65
+ end