jet_black 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9d0380fc2fc0e5a1a92c769bd696b8b6ed50ff314299f41dee11b9a1637ba7af
4
+ data.tar.gz: 2e5d5cef1ba46569904b89cd4af6893cad9b7041bcba910fe987ba885441cd32
5
+ SHA512:
6
+ metadata.gz: 586f355aa9142484c739bde06cd412e8096932b7557e28fed5c93c98f850263517226139592308cf0063447da12d457494a859a852e660ac9f2585f1aaa0b81c
7
+ data.tar.gz: aac1249839146336bb8a414b01073bbfb70ff457a27b3160094c868b61a92b798c578f38e2ae73d5ca126a14ae5b9d42fa71aafa4cfcd612a1d5ab4dcb6a0f57
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.3
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in jet_black.gemspec
4
+ gemspec
@@ -0,0 +1,41 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ jet_black (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.2)
10
+ diff-lcs (1.3)
11
+ method_source (0.9.0)
12
+ pry (0.11.3)
13
+ coderay (~> 1.1.0)
14
+ method_source (~> 0.9.0)
15
+ rake (10.5.0)
16
+ rspec (3.7.0)
17
+ rspec-core (~> 3.7.0)
18
+ rspec-expectations (~> 3.7.0)
19
+ rspec-mocks (~> 3.7.0)
20
+ rspec-core (3.7.1)
21
+ rspec-support (~> 3.7.0)
22
+ rspec-expectations (3.7.0)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.7.0)
25
+ rspec-mocks (3.7.0)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.7.0)
28
+ rspec-support (3.7.1)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ bundler (~> 1.16)
35
+ jet_black!
36
+ pry
37
+ rake (~> 10.0)
38
+ rspec (~> 3.0)
39
+
40
+ BUNDLED WITH
41
+ 1.16.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Oli Peate
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,12 @@
1
+ # JetBlack
2
+
3
+ A black-box testing utility for Ruby. Features:
4
+
5
+ - Each session runs in a temporary directory
6
+ - Captures stdout, stderr and exit status of a command
7
+ - Keeps a history of commands executed
8
+ - Temporarily overriding environment variables for a command
9
+ - Copying fixture files to the temporary directory
10
+ - Appending content to files in the temporary directory
11
+ - Adding a path prefix to include your executable
12
+ - Option to escape the Bundler environment, to "bundle exec" new Gemfiles
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env bash
2
+
3
+ echo "Hello from example bin"
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,25 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "jet_black/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "jet_black"
7
+ spec.version = JetBlack::VERSION
8
+ spec.authors = ["Oli Peate"]
9
+
10
+ spec.summary = "Black box testing"
11
+ spec.homepage = "https://github.com/odlp/jet_black"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
15
+ f.match(%r{^(test|spec|features)/})
16
+ end
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.16"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "pry"
25
+ end
@@ -0,0 +1,17 @@
1
+ require "jet_black/configuration"
2
+ require "jet_black/session"
3
+ require "jet_black/version"
4
+
5
+ module JetBlack
6
+ def self.configuration
7
+ @configuration ||= Configuration.new
8
+ end
9
+
10
+ def self.configure
11
+ yield configuration if block_given?
12
+ end
13
+
14
+ def self.reset!
15
+ @configuration = Configuration.new
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ module JetBlack
2
+ class Configuration
3
+ attr_accessor :fixture_directory, :path_prefix
4
+ end
5
+ end
@@ -0,0 +1,34 @@
1
+ module JetBlack
2
+ class Environment
3
+ def initialize(raw_env)
4
+ @raw_env = raw_env.dup
5
+ end
6
+
7
+ def to_h
8
+ apply_path_prefix(stringify_env(raw_env))
9
+ end
10
+
11
+ private
12
+
13
+ attr_reader :raw_env
14
+
15
+ def stringify_env(env)
16
+ env.map do |key, value|
17
+ [key.to_s, value.to_s]
18
+ end.to_h
19
+ end
20
+
21
+ def apply_path_prefix(env)
22
+ if path_prefix&.empty?
23
+ env
24
+ else
25
+ env["PATH"] = [path_prefix, ENV["PATH"]].join(File::PATH_SEPARATOR)
26
+ env
27
+ end
28
+ end
29
+
30
+ def path_prefix
31
+ JetBlack.configuration.path_prefix
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,34 @@
1
+ module JetBlack
2
+ class Error < ::StandardError
3
+ end
4
+
5
+ class InvalidPathError < Error
6
+ attr_reader :raw_path, :expanded_path
7
+
8
+ def initialize(raw_path, expanded_path)
9
+ @raw_path = raw_path
10
+ @expanded_path = expanded_path
11
+
12
+ super <<~MSG
13
+ Please specify a relative path within the temp dir.
14
+ Raw path: '#{raw_path}'
15
+ Expanded path: '#{expanded_path}'
16
+ MSG
17
+ end
18
+ end
19
+
20
+ class NonExistentFileError < Error
21
+ attr_reader :raw_path, :expanded_path
22
+
23
+ def initialize(raw_path, expanded_path)
24
+ @raw_path = raw_path
25
+ @expanded_path = expanded_path
26
+
27
+ super <<~MSG
28
+ Please create the file before trying to append content.
29
+ Raw path: '#{raw_path}'
30
+ Expanded path: '#{expanded_path}'
31
+ MSG
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,12 @@
1
+ module JetBlack
2
+ class ExecutedCommand
3
+ attr_reader :raw_command, :stdout, :stderr, :exit_status
4
+
5
+ def initialize(raw_command:, stdout:, stderr:, exit_status:)
6
+ @raw_command = raw_command
7
+ @stdout = stdout.chomp
8
+ @stderr = stderr.chomp
9
+ @exit_status = exit_status.to_i
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require "open3"
5
+ require "tmpdir"
6
+ require_relative "environment"
7
+ require_relative "errors"
8
+ require_relative "executed_command"
9
+
10
+ module JetBlack
11
+ class Session
12
+ attr_reader :commands
13
+
14
+ def initialize(options: {})
15
+ @commands = []
16
+ @session_options = options
17
+ end
18
+
19
+ def run(command, env: {}, options: {})
20
+ combined_options = session_options.merge(options)
21
+ executed_command = exec_command(command, env, combined_options)
22
+ commands << executed_command
23
+ executed_command
24
+ end
25
+
26
+ def directory
27
+ @_directory ||= File.realpath(Dir.mktmpdir("jet_black"))
28
+ end
29
+
30
+ def create_file(file_path, file_content)
31
+ expanded_file_path = File.expand_path(file_path, directory)
32
+ expanded_dir = File.dirname(expanded_file_path)
33
+
34
+ unless expanded_file_path.start_with?(directory)
35
+ raise JetBlack::InvalidPathError.new(file_path, expanded_file_path)
36
+ end
37
+
38
+ FileUtils.mkdir_p(expanded_dir)
39
+ File.write(expanded_file_path, file_content)
40
+ end
41
+
42
+ def append_to_file(file_path, append_content)
43
+ expanded_file_path = File.expand_path(file_path, directory)
44
+
45
+ unless File.exist?(expanded_file_path)
46
+ raise JetBlack::NonExistentFileError.new(file_path, expanded_file_path)
47
+ end
48
+
49
+ File.open(expanded_file_path, "a") do |file|
50
+ file.write(append_content)
51
+ end
52
+ end
53
+
54
+ def copy_fixture(source_path, destination_path)
55
+ src_fixture_dir = JetBlack.configuration.fixture_directory
56
+ expanded_source_path = File.expand_path(source_path, src_fixture_dir)
57
+ expanded_destination_path = File.expand_path(destination_path, directory)
58
+ expanded_destination_dir = File.dirname(expanded_destination_path)
59
+
60
+ if src_fixture_dir.nil?
61
+ raise Error.new("Please configure the fixture_directory")
62
+ end
63
+
64
+ unless expanded_destination_path.start_with?(directory)
65
+ raise JetBlack::InvalidPathError.new(
66
+ destination_path, expanded_destination_path
67
+ )
68
+ end
69
+
70
+ FileUtils.mkdir_p(expanded_destination_dir)
71
+ FileUtils.cp(expanded_source_path, expanded_destination_path)
72
+ end
73
+
74
+ private
75
+
76
+ attr_reader :session_options
77
+
78
+ def exec_command(raw_command, raw_env, options)
79
+ env = Environment.new(raw_env).to_h
80
+
81
+ command_context(options) do
82
+ stdout, stderr, exit_status =
83
+ Open3.capture3(env, raw_command, chdir: directory)
84
+
85
+ ExecutedCommand.new(
86
+ raw_command: raw_command,
87
+ stdout: stdout,
88
+ stderr: stderr,
89
+ exit_status: exit_status,
90
+ )
91
+ end
92
+ end
93
+
94
+ def command_context(options)
95
+ if options[:clean_bundler_env]
96
+ Bundler.with_original_env { yield }
97
+ else
98
+ yield
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,3 @@
1
+ module JetBlack
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jet_black
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Oli Peate
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-04-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
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:
70
+ email:
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - ".gitignore"
76
+ - ".rspec"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/jet_black_bin_example
84
+ - bin/setup
85
+ - jet_black.gemspec
86
+ - lib/jet_black.rb
87
+ - lib/jet_black/configuration.rb
88
+ - lib/jet_black/environment.rb
89
+ - lib/jet_black/errors.rb
90
+ - lib/jet_black/executed_command.rb
91
+ - lib/jet_black/session.rb
92
+ - lib/jet_black/version.rb
93
+ homepage: https://github.com/odlp/jet_black
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.7.6
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Black box testing
117
+ test_files: []