From 816ea5587bdf551cf9bd05da7c7400210e5d26d0 Mon Sep 17 00:00:00 2001 From: Tom Brand Date: Sat, 3 Mar 2018 15:22:14 +0000 Subject: [PATCH] Add tests for running tests I ultimately want to add python/pytest support but felt that the code in `lib/functions.vim` was too poorly factored to reasonably extend. This is a start in the direction of adding tests to this repo and is in no way finished. I want to push this for some early feedback. Specifically it would be good to get feedback on: * Should `lib/functions.vim` be extended? Maybe it would be better to use [vim-test](https://github.com/janko-m/vim-test) and add vipe support to vim-test. * Is it worth while backfilling tests? Are there other areas of the project that would benefit from testing? * What is the best way to get these tests working on ci? --- tests/.gitignore | 2 + tests/.rspec | 1 + tests/Gemfile | 4 ++ tests/Gemfile.lock | 28 ++++++++ tests/README.md | 23 +++++++ tests/spec/fixtures/ruby/rspec_spec.rb | 0 tests/spec/fixtures/ruby_with_bundler/Gemfile | 0 .../fixtures/ruby_with_bundler/rspec_spec.rb | 0 tests/spec/functions.vim/run_tests_spec.rb | 67 +++++++++++++++++++ tests/spec/spec_helper.rb | 23 +++++++ 10 files changed, 148 insertions(+) create mode 100644 tests/.gitignore create mode 100644 tests/.rspec create mode 100644 tests/Gemfile create mode 100644 tests/Gemfile.lock create mode 100644 tests/README.md create mode 100644 tests/spec/fixtures/ruby/rspec_spec.rb create mode 100644 tests/spec/fixtures/ruby_with_bundler/Gemfile create mode 100644 tests/spec/fixtures/ruby_with_bundler/rspec_spec.rb create mode 100644 tests/spec/functions.vim/run_tests_spec.rb create mode 100644 tests/spec/spec_helper.rb diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 0000000..f421c4c --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1,2 @@ +.bundle/ +bin/ diff --git a/tests/.rspec b/tests/.rspec new file mode 100644 index 0000000..c99d2e7 --- /dev/null +++ b/tests/.rspec @@ -0,0 +1 @@ +--require spec_helper diff --git a/tests/Gemfile b/tests/Gemfile new file mode 100644 index 0000000..69939bf --- /dev/null +++ b/tests/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +gem 'rspec', '~> 3' +gem 'vimrunner', '~> 0.3' diff --git a/tests/Gemfile.lock b/tests/Gemfile.lock new file mode 100644 index 0000000..945c462 --- /dev/null +++ b/tests/Gemfile.lock @@ -0,0 +1,28 @@ +GEM + remote: https://rubygems.org/ + specs: + diff-lcs (1.3) + rspec (3.7.0) + rspec-core (~> 3.7.0) + rspec-expectations (~> 3.7.0) + rspec-mocks (~> 3.7.0) + rspec-core (3.7.1) + rspec-support (~> 3.7.0) + rspec-expectations (3.7.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.7.0) + rspec-mocks (3.7.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.7.0) + rspec-support (3.7.1) + vimrunner (0.3.4) + +PLATFORMS + ruby + +DEPENDENCIES + rspec (~> 3) + vimrunner (~> 0.3) + +BUNDLED WITH + 1.16.0 diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 0000000..55ce26b --- /dev/null +++ b/tests/README.md @@ -0,0 +1,23 @@ +# Tests # + +The motivation at the time of writing is to be able to refactor +`lib/functions.vim` test running functions so that they can be extended. For example +it would be nice to add python/pytest running without hacking in yet more code to this +file. + +## Setup ## + +Currently the tests depend on [vimrunner](https://github.com/AndrewRadev/vimrunner) +and have only been run against macvim. The reason for macvim is it has a reliable +vim client server implementation that normal vim does not, at least not vim with +xquartz on macos. + +### Install macvim ### + + brew cask install macvim + +## Run tests ## + + cd tests + bundle install + bundle exec rspec spec diff --git a/tests/spec/fixtures/ruby/rspec_spec.rb b/tests/spec/fixtures/ruby/rspec_spec.rb new file mode 100644 index 0000000..e69de29 diff --git a/tests/spec/fixtures/ruby_with_bundler/Gemfile b/tests/spec/fixtures/ruby_with_bundler/Gemfile new file mode 100644 index 0000000..e69de29 diff --git a/tests/spec/fixtures/ruby_with_bundler/rspec_spec.rb b/tests/spec/fixtures/ruby_with_bundler/rspec_spec.rb new file mode 100644 index 0000000..e69de29 diff --git a/tests/spec/functions.vim/run_tests_spec.rb b/tests/spec/functions.vim/run_tests_spec.rb new file mode 100644 index 0000000..ba38930 --- /dev/null +++ b/tests/spec/functions.vim/run_tests_spec.rb @@ -0,0 +1,67 @@ +describe 'running tests with luan vim' do + before do + if vipe_file.exist? + vipe_file.delete + end + end + + let(:vim_with_functions) { + vim.source("#{ENV['HOME']}/.vim/plugged/vipe/plugin/vipe.vim") + vim.source("#{ENV['HOME']}/.vim/lib/functions.vim") + + dir, filename = test_path.split + vim.command("cd #{dir}") + vim.edit(filename) + vim + } + + let(:vipe_file) { + Pathname.new('/tmp') + .join(".vipe_pipe_#{test_path.dirname.to_s.gsub('/', '_')}") + } + + let(:last_command) { + vipe_file.readlines.last.chomp + } + + let(:fixture_path) { + Pathname.new(ENV['HOME']).join( + '.vim', + 'tests', + 'spec', + 'fixtures' + ) + } + + def run_test_file(vim) + vim.command('call RunTestFile()') + end + + context 'editing an rspec file' do + let(:test_path) { + fixture_path.join( + 'ruby', + 'rspec_spec.rb' + ) + } + + it 'runs the rspec command' do + run_test_file(vim_with_functions) + expect(last_command).to eq('rspec --color rspec_spec.rb') + end + end + + context 'editing an rspec file in a bundler environment' do + let(:test_path) { + fixture_path.join( + 'ruby_with_bundler', + 'rspec_spec.rb' + ) + } + + it 'runs the rspec command in the bundler environment' do + run_test_file(vim_with_functions) + expect(last_command).to eq('bundle exec rspec --color rspec_spec.rb') + end + end +end diff --git a/tests/spec/spec_helper.rb b/tests/spec/spec_helper.rb new file mode 100644 index 0000000..990ceb6 --- /dev/null +++ b/tests/spec/spec_helper.rb @@ -0,0 +1,23 @@ +require 'vimrunner' +require 'vimrunner/rspec' + +# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + config.expect_with :rspec do |expectations| + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + config.mock_with :rspec do |mocks| + mocks.verify_partial_doubles = true + end + + config.shared_context_metadata_behavior = :apply_to_host_groups +end + +Vimrunner::RSpec.configure do |config| + config.reuse_server = true + config.start_vim do + vim = Vimrunner.start_gvim + vim + end +end