├── .coveralls.yml ├── .github ├── dependabot.yml └── workflows │ ├── pages.yml │ └── test.yml ├── .gitignore ├── .rspec ├── .yardopts ├── CHANGELOG.md ├── Gemfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── lib └── rspec │ ├── temp_dir.rb │ └── temp_dir │ ├── uses_temp_dir.rb │ ├── version.rb │ └── within_temp_dir.rb ├── rspec-temp_dir.gemspec └── spec ├── rspec ├── uses_temp_dir_spec.rb └── within_temp_dir_spec.rb └── spec_helper.rb /.coveralls.yml: -------------------------------------------------------------------------------- 1 | repo_token: neej8FqJspOAXAEtwqYge65Dllr8Jn9R5 2 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # c.f. https://docs.github.com/en/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file 2 | version: 2 3 | 4 | updates: 5 | - package-ecosystem: github-actions 6 | directory: / 7 | schedule: 8 | interval: weekly 9 | assignees: 10 | - sue445 11 | -------------------------------------------------------------------------------- /.github/workflows/pages.yml: -------------------------------------------------------------------------------- 1 | # Simple workflow for deploying static content to GitHub Pages 2 | name: Deploy static content to Pages 3 | 4 | on: 5 | # Runs on pushes targeting the default branch 6 | push: 7 | branches: 8 | - master 9 | 10 | # Allows you to run this workflow manually from the Actions tab 11 | workflow_dispatch: 12 | 13 | # Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages 14 | permissions: 15 | contents: read 16 | pages: write 17 | id-token: write 18 | 19 | # Allow one concurrent deployment 20 | concurrency: 21 | group: "pages" 22 | cancel-in-progress: true 23 | 24 | jobs: 25 | # Single deploy job since we're just deploying 26 | deploy: 27 | environment: 28 | name: github-pages 29 | url: ${{ steps.deployment.outputs.page_url }} 30 | runs-on: ubuntu-latest 31 | steps: 32 | - name: Checkout 33 | uses: actions/checkout@v4 34 | 35 | - uses: ruby/setup-ruby@v1 36 | with: 37 | ruby-version: ruby 38 | bundler-cache: true 39 | 40 | - run: bundle exec yard 41 | 42 | - name: Setup Pages 43 | uses: actions/configure-pages@v5 44 | - name: Upload artifact 45 | uses: actions/upload-pages-artifact@v3 46 | with: 47 | # Upload entire repository 48 | path: './doc' 49 | - name: Deploy to GitHub Pages 50 | id: deployment 51 | uses: actions/deploy-pages@main 52 | 53 | - name: Slack Notification (not success) 54 | uses: act10ns/slack@v2 55 | if: "! success()" 56 | continue-on-error: true 57 | with: 58 | status: ${{ job.status }} 59 | webhook-url: ${{ secrets.SLACK_WEBHOOK }} 60 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: test 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | types: 9 | - opened 10 | - synchronize 11 | - reopened 12 | schedule: 13 | - cron: "0 10 * * 5" # JST 19:00 (Fri) 14 | 15 | jobs: 16 | test: 17 | runs-on: ubuntu-latest 18 | 19 | strategy: 20 | fail-fast: false 21 | 22 | matrix: 23 | ruby: 24 | - "2.0" 25 | - "2.1" 26 | - "2.2" 27 | - "2.3" 28 | - "2.4" 29 | - "2.5" 30 | - "2.6" 31 | - "2.7" 32 | - "3.0" 33 | - "3.1" 34 | - "3.2" 35 | - "3.3" 36 | - "3.4" 37 | 38 | steps: 39 | - uses: actions/checkout@v4 40 | 41 | - uses: ruby/setup-ruby@v1 42 | with: 43 | ruby-version: ${{ matrix.ruby }} 44 | bundler-cache: true 45 | 46 | - run: bundle update --jobs $(nproc) --retry 3 47 | 48 | - name: Setup Code Climate Test Reporter 49 | uses: aktions/codeclimate-test-reporter@v1 50 | with: 51 | codeclimate-test-reporter-id: ${{ secrets.CC_TEST_REPORTER_ID }} 52 | command: before-build 53 | continue-on-error: true 54 | 55 | - run: bundle exec rspec 56 | 57 | - name: Teardown Code Climate Test Reporter 58 | uses: aktions/codeclimate-test-reporter@v1 59 | with: 60 | codeclimate-test-reporter-id: ${{ secrets.CC_TEST_REPORTER_ID }} 61 | command: after-build 62 | if: always() 63 | continue-on-error: true 64 | 65 | - name: Slack Notification (not success) 66 | uses: act10ns/slack@v2 67 | if: "! success()" 68 | continue-on-error: true 69 | with: 70 | status: ${{ job.status }} 71 | webhook-url: ${{ secrets.SLACK_WEBHOOK }} 72 | matrix: ${{ toJson(matrix) }} 73 | 74 | notify: 75 | needs: 76 | - test 77 | 78 | runs-on: ubuntu-latest 79 | 80 | steps: 81 | - name: Slack Notification (success) 82 | uses: act10ns/slack@v2 83 | if: always() 84 | continue-on-error: true 85 | with: 86 | status: ${{ job.status }} 87 | webhook-url: ${{ secrets.SLACK_WEBHOOK }} 88 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | Gemfile.lock 7 | InstalledFiles 8 | _yardoc 9 | coverage 10 | doc/ 11 | lib/bundler/man 12 | pkg 13 | rdoc 14 | spec/reports 15 | test/tmp 16 | test/version_tmp 17 | tmp 18 | *.bundle 19 | *.so 20 | *.o 21 | *.a 22 | mkmf.log 23 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --format documentation 2 | --color 3 | --require spec_helper 4 | -------------------------------------------------------------------------------- /.yardopts: -------------------------------------------------------------------------------- 1 | --markup markdown 2 | --no-private 3 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | ## master 3 | [full changelog](http://github.com/sue445/rspec-temp_dir/compare/v1.1.1...master) 4 | 5 | ## v1.1.1 6 | [full changelog](http://github.com/sue445/rspec-temp_dir/compare/v1.1.0...v1.1.1) 7 | 8 | * Enable MFA requirement for gem releasing 9 | * https://github.com/sue445/rspec-temp_dir/pull/35 10 | 11 | ## v1.1.0 12 | [full changelog](http://github.com/sue445/rspec-temp_dir/compare/v1.0.0...v1.1.0) 13 | 14 | * Impl within temp dir 15 | * https://github.com/sue445/rspec-temp_dir/pull/13 16 | 17 | ## v1.0.0 18 | [full changelog](http://github.com/sue445/rspec-temp_dir/compare/v0.0.4...v1.0.0) 19 | 20 | * Drop support ruby 1.9 21 | * https://github.com/sue445/rspec-temp_dir/pull/10 22 | * Drop support rspec 2.x 23 | * https://github.com/sue445/rspec-temp_dir/pull/9 24 | 25 | ## v0.0.4 26 | [full changelog](http://github.com/sue445/rspec-temp_dir/compare/v0.0.3...v0.0.4) 27 | 28 | * Fixed no method error for Pathname ruby 2.2.2 29 | * https://github.com/sue445/rspec-temp_dir/pull/5 30 | 31 | ## v0.0.3 32 | [full changelog](http://github.com/sue445/rspec-temp_dir/compare/v0.0.2...v0.0.3) 33 | 34 | * support `temp_dir_path` 35 | * https://github.com/sue445/rspec-temp_dir/pull/2 36 | 37 | ## v0.0.2 38 | [full changelog](http://github.com/sue445/rspec-temp_dir/compare/v0.0.1...v0.0.2) 39 | 40 | * bugfix: raise error when padrino and rspec3+ 41 | * https://github.com/sue445/rspec-temp_dir/pull/1 42 | 43 | ## v0.0.1 44 | * first release 45 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in rspec-temp_dir.gemspec 4 | gemspec 5 | 6 | if Gem::Version.create(RUBY_VERSION) < Gem::Version.create("2.2.0") 7 | # NOTE: rake v13.0.0+ requires Ruby 2.2.0+ 8 | gem "rake", "< 13.0.0" 9 | end 10 | 11 | if Gem::Version.create(RUBY_VERSION) < Gem::Version.create("2.3.0") 12 | # NOTE: json 2.6.0+ requires Ruby 2.3.0+ 13 | gem "json", "< 2.6.0" 14 | 15 | # NOTE: webrick 1.4.0+ requires Ruby 2.3.0+ 16 | gem "webrick", "< 1.4.0" 17 | end 18 | 19 | if Gem::Version.create(RUBY_VERSION) < Gem::Version.create("2.4.0") 20 | # NOTE: simplecov-html v0.11.0+ requires Ruby ~> 2.4 21 | gem "simplecov-html", "< 0.11.0" 22 | end 23 | 24 | if Gem::Version.create(RUBY_VERSION) < Gem::Version.create("2.5.0") 25 | # NOTE: docile v1.4.0+ requires Ruby 2.5.0+ 26 | gem "docile", "< 1.4.0" 27 | end 28 | 29 | if Gem::Version.create(RUBY_VERSION) < Gem::Version.create("2.7.0") 30 | # term-ansicolor 1.9.0+ doesn't work on Ruby < 2.7 31 | gem "term-ansicolor", "< 1.9.0" 32 | end 33 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 sue445 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rspec::TempDir 2 | 3 | create automatically temporary directory at each examples 4 | 5 | This is inspired by Junit [TemporaryFolder](http://junit.org/junit4/javadoc/4.12/org/junit/rules/TemporaryFolder.html) 6 | 7 | [![Gem Version](https://badge.fury.io/rb/rspec-temp_dir.svg)](http://badge.fury.io/rb/rspec-temp_dir) 8 | [![test](https://github.com/sue445/rspec-temp_dir/actions/workflows/test.yml/badge.svg)](https://github.com/sue445/rspec-temp_dir/actions/workflows/test.yml) 9 | [![Code Climate](https://codeclimate.com/github/sue445/rspec-temp_dir.png)](https://codeclimate.com/github/sue445/rspec-temp_dir) 10 | [![Coverage Status](https://img.shields.io/coveralls/sue445/rspec-temp_dir.svg)](https://coveralls.io/r/sue445/rspec-temp_dir) 11 | 12 | ## Requirements 13 | * ruby 2.0+ 14 | * rspec 3.0.0+ 15 | 16 | ## Installation 17 | 18 | Add this line to your application's Gemfile: 19 | 20 | gem 'rspec-temp_dir' 21 | 22 | And then execute: 23 | 24 | $ bundle 25 | 26 | Or install it yourself as: 27 | 28 | $ gem install rspec-temp_dir 29 | 30 | ## Usage 31 | When use `include_context "uses temp dir"` , create automatically and can use `temp_dir` 32 | 33 | ```ruby 34 | require 'rspec/temp_dir' 35 | 36 | describe "uses temp dir" do 37 | include_context "uses temp dir" 38 | 39 | it "should create temp_dir" do 40 | expect(Pathname(temp_dir)).to be_exist 41 | end 42 | 43 | it "can create file in temp_dir" do 44 | temp_file = "#{temp_dir}/temp.txt" 45 | 46 | File.open(temp_file, "w") do |f| 47 | f.write("foo") 48 | end 49 | 50 | expect(File.read(temp_file)).to eq "foo" 51 | end 52 | 53 | describe "#temp_dir_path" do 54 | subject{ temp_dir_path } 55 | 56 | it { should be_an_instance_of Pathname } 57 | it { should be_exist } 58 | end 59 | end 60 | 61 | describe "within temp dir" do 62 | # create temp dir and cd to temp dir 63 | include_context "within temp dir" 64 | 65 | it "within temp dir" do 66 | expect(Dir.pwd).to eq temp_dir 67 | end 68 | end 69 | ``` 70 | 71 | ## Contributing 72 | 73 | 1. Fork it ( https://github.com/sue445/rspec-temp_dir/fork ) 74 | 2. Create your feature branch (`git checkout -b my-new-feature`) 75 | 3. Commit your changes (`git commit -am 'Add some feature'`) 76 | 4. Push to the branch (`git push origin my-new-feature`) 77 | 5. Create a new Pull Request 78 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | require "rspec/core/rake_task" 3 | 4 | RSpec::Core::RakeTask.new(:spec) 5 | 6 | task :default => :spec 7 | 8 | -------------------------------------------------------------------------------- /lib/rspec/temp_dir.rb: -------------------------------------------------------------------------------- 1 | require "rspec/temp_dir/version" 2 | require "rspec/temp_dir/uses_temp_dir" 3 | require "rspec/temp_dir/within_temp_dir" 4 | 5 | module Rspec 6 | module TempDir 7 | # Your code goes here... 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /lib/rspec/temp_dir/uses_temp_dir.rb: -------------------------------------------------------------------------------- 1 | require "tmpdir" 2 | require "rspec" 3 | require "pathname" 4 | 5 | RSpec.shared_context "uses temp dir" do 6 | around do |example| 7 | Dir.mktmpdir("rspec-") do |dir| 8 | @temp_dir = dir 9 | example.run 10 | end 11 | end 12 | 13 | attr_reader :temp_dir 14 | 15 | def temp_dir_path 16 | Pathname(temp_dir) 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /lib/rspec/temp_dir/version.rb: -------------------------------------------------------------------------------- 1 | module Rspec 2 | module TempDir 3 | VERSION = "1.1.1" 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /lib/rspec/temp_dir/within_temp_dir.rb: -------------------------------------------------------------------------------- 1 | require "rspec" 2 | 3 | RSpec.shared_context "within temp dir" do 4 | include_context "uses temp dir" 5 | 6 | around do |example| 7 | Dir.chdir(temp_dir) do 8 | example.run 9 | end 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /rspec-temp_dir.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'rspec/temp_dir/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "rspec-temp_dir" 8 | spec.version = Rspec::TempDir::VERSION 9 | spec.authors = ["sue445"] 10 | spec.email = ["sue445@sue445.net"] 11 | spec.summary = %q{create automatically temporary directory at each examples} 12 | spec.description = %q{create automatically temporary directory at each examples} 13 | spec.homepage = "https://github.com/sue445/rspec-temp_dir" 14 | spec.license = "MIT" 15 | 16 | spec.metadata["homepage_uri"] = spec.homepage 17 | spec.metadata["source_code_uri"] = spec.homepage 18 | spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/master/CHANGELOG.md" 19 | spec.metadata["documentation_uri"] = "https://sue445.github.io/rspec-temp_dir/" 20 | spec.metadata["rubygems_mfa_required"] = "true" 21 | 22 | spec.files = `git ls-files -z`.split("\x0") 23 | spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } 24 | spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) 25 | spec.require_paths = ["lib"] 26 | 27 | spec.required_ruby_version = ">= 2.0.0" 28 | 29 | spec.add_dependency "rspec", ">= 3.0" 30 | 31 | spec.add_development_dependency "bundler" 32 | spec.add_development_dependency "coveralls_reborn" 33 | spec.add_development_dependency "rake" 34 | spec.add_development_dependency "simplecov" 35 | spec.add_development_dependency "term-ansicolor", "!= 1.11.1" # ref. https://github.com/flori/term-ansicolor/issues/41 36 | spec.add_development_dependency "yard" 37 | end 38 | -------------------------------------------------------------------------------- /spec/rspec/uses_temp_dir_spec.rb: -------------------------------------------------------------------------------- 1 | describe "uses temp dir" do 2 | include_context "uses temp dir" 3 | 4 | it "should create temp_dir" do 5 | expect(Pathname(temp_dir)).to be_exist 6 | end 7 | 8 | it "can create file in temp_dir" do 9 | temp_file = "#{temp_dir}/temp.txt" 10 | 11 | File.open(temp_file, "w") do |f| 12 | f.write("foo") 13 | end 14 | 15 | expect(File.read(temp_file)).to eq "foo" 16 | end 17 | 18 | describe "#temp_dir_path" do 19 | subject{ temp_dir_path } 20 | 21 | it { should be_an_instance_of Pathname } 22 | it { should be_exist } 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /spec/rspec/within_temp_dir_spec.rb: -------------------------------------------------------------------------------- 1 | require "rbconfig" 2 | 3 | describe "within temp dir" do 4 | include_context "within temp dir" 5 | 6 | it "within temp dir" do 7 | expect(current_dir).to eq temp_dir 8 | end 9 | 10 | def current_dir 11 | dir = Dir.pwd 12 | 13 | case RbConfig::CONFIG["host_os"] 14 | when /darwin|mac os/ 15 | # FIXME: `Dir.mktmpdir` returns "/var/folders/xxx", but `pwd` returns "/private/var/folders/xxx" ... 16 | dir.gsub!(%r{^/private/}, "/") 17 | end 18 | 19 | dir 20 | end 21 | end 22 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # This file was generated by the `rspec --init` command. Conventionally, all 2 | # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. 3 | # The generated `.rspec` file contains `--require spec_helper` which will cause this 4 | # file to always be loaded, without a need to explicitly require it in any files. 5 | # 6 | # Given that it is always loaded, you are encouraged to keep this file as 7 | # light-weight as possible. Requiring heavyweight dependencies from this file 8 | # will add to the boot time of your test suite on EVERY test run, even for an 9 | # individual file that may not need all of that loaded. Instead, make a 10 | # separate helper file that requires this one and then use it only in the specs 11 | # that actually need it. 12 | # 13 | # The `.rspec` file also contains a few flags that are not defaults but that 14 | # users commonly want. 15 | # 16 | # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration 17 | 18 | if ENV["CI"] 19 | require "simplecov" 20 | require "coveralls" 21 | 22 | SimpleCov.formatter = Coveralls::SimpleCov::Formatter 23 | SimpleCov.start 24 | end 25 | 26 | $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) 27 | require 'rspec/temp_dir' 28 | 29 | RSpec.configure do |config| 30 | # The settings below are suggested to provide a good initial experience 31 | # with RSpec, but feel free to customize to your heart's content. 32 | =begin 33 | # These two settings work together to allow you to limit a spec run 34 | # to individual examples or groups you care about by tagging them with 35 | # `:focus` metadata. When nothing is tagged with `:focus`, all examples 36 | # get run. 37 | config.filter_run :focus 38 | config.run_all_when_everything_filtered = true 39 | 40 | # Many RSpec users commonly either run the entire suite or an individual 41 | # file, and it's useful to allow more verbose output when running an 42 | # individual spec file. 43 | if config.files_to_run.one? 44 | # Use the documentation formatter for detailed output, 45 | # unless a formatter has already been configured 46 | # (e.g. via a command-line flag). 47 | config.default_formatter = 'doc' 48 | end 49 | 50 | # Print the 10 slowest examples and example groups at the 51 | # end of the spec run, to help surface which specs are running 52 | # particularly slow. 53 | config.profile_examples = 10 54 | 55 | # Run specs in random order to surface order dependencies. If you find an 56 | # order dependency and want to debug it, you can fix the order by providing 57 | # the seed, which is printed after each run. 58 | # --seed 1234 59 | config.order = :random 60 | 61 | # Seed global randomization in this process using the `--seed` CLI option. 62 | # Setting this allows you to use `--seed` to deterministically reproduce 63 | # test failures related to randomization by passing the same `--seed` value 64 | # as the one that triggered the failure. 65 | Kernel.srand config.seed 66 | 67 | # rspec-expectations config goes here. You can use an alternate 68 | # assertion/expectation library such as wrong or the stdlib/minitest 69 | # assertions if you prefer. 70 | config.expect_with :rspec do |expectations| 71 | # Enable only the newer, non-monkey-patching expect syntax. 72 | # For more details, see: 73 | # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax 74 | expectations.syntax = :expect 75 | end 76 | 77 | # rspec-mocks config goes here. You can use an alternate test double 78 | # library (such as bogus or mocha) by changing the `mock_with` option here. 79 | config.mock_with :rspec do |mocks| 80 | # Enable only the newer, non-monkey-patching expect syntax. 81 | # For more details, see: 82 | # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ 83 | mocks.syntax = :expect 84 | 85 | # Prevents you from mocking or stubbing a method that does not exist on 86 | # a real object. This is generally recommended. 87 | mocks.verify_partial_doubles = true 88 | end 89 | =end 90 | 91 | config.order = :random 92 | end 93 | --------------------------------------------------------------------------------