diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..1eef948 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,22 @@ +name: CI + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + ruby: ["3.2", "3.3", "3.4", "4.0"] + steps: + - uses: actions/checkout@v4 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + bundler-cache: true + - name: Run tests + run: bundle exec bin/test diff --git a/Gemfile.lock b/Gemfile.lock index ca8b440..a311a82 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,26 +1,28 @@ PATH remote: . specs: - testerobly (1.1.1) + testerobly (1.1.2) listen (~> 3.9.0) + logger (~> 1.7) GEM remote: https://rubygems.org/ specs: ffi (1.17.2) - ffi (1.17.2-arm64-darwin) listen (3.9.0) rb-fsevent (~> 0.10, >= 0.10.3) rb-inotify (~> 0.9, >= 0.9.10) + logger (1.7.0) + minitest (5.27.0) rb-fsevent (0.11.2) rb-inotify (0.11.1) ffi (~> 1.0) PLATFORMS - arm64-darwin-24 ruby DEPENDENCIES + minitest (~> 5.0) testerobly! BUNDLED WITH diff --git a/bin/test b/bin/test new file mode 100755 index 0000000..5496809 --- /dev/null +++ b/bin/test @@ -0,0 +1,15 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +test_files = if ARGV.empty? + Dir["test/**/*_test.rb"] +else + ARGV +end + +if test_files.empty? + warn "No test files found." + exit 1 +end + +exec "ruby", "-Itest", *test_files diff --git a/config/testerobly.rb b/config/testerobly.rb index 2d3b83f..6a7eab8 100644 --- a/config/testerobly.rb +++ b/config/testerobly.rb @@ -1,6 +1,6 @@ Testerobly.configure do |config| - config.test_command = "ruby %s" - config.test_all_command = "echo 'sample test all command'" + config.test_command = "bin/test %s" + config.test_all_command = "bin/test" config.on_change = Proc.new do |path, tests| if path == "config/testerobly.rb" tests << "test/custom_on_change.rb" diff --git a/test/custom_on_change.rb b/test/custom_on_change.rb deleted file mode 100644 index 8274e43..0000000 --- a/test/custom_on_change.rb +++ /dev/null @@ -1,4 +0,0 @@ -puts "custom_on_change.rb here" -# require "debug" -# debugger - diff --git a/test/custom_on_change_test.rb b/test/custom_on_change_test.rb new file mode 100644 index 0000000..65a0ebf --- /dev/null +++ b/test/custom_on_change_test.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require_relative "test_helper" + +class CustomOnChangeTest < Minitest::Test + def test_custom_on_change_is_runnable + assert_equal 2, 1 + 1 + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..8861356 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +require "bundler/setup" +require "minitest/autorun" +require "fileutils" +require "tmpdir" + +require "testerobly" diff --git a/test/testerobly_test.rb b/test/testerobly_test.rb index 095809b..df78b2c 100644 --- a/test/testerobly_test.rb +++ b/test/testerobly_test.rb @@ -1 +1,103 @@ -puts "testerobly_test.rb here" +# frozen_string_literal: true + +require_relative "test_helper" + +class TesteroblyTest < Minitest::Test + def setup + @original_configuration = Testerobly.configuration + Testerobly.configuration = Testerobly::Configuration.new + end + + def teardown + Testerobly.configuration = @original_configuration + end + + def test_configuration_defaults_and_bind + config = Testerobly.configuration + + assert_equal "bin/test %s", config.test_command + assert_equal({}, config.keys) + assert_nil config.on_change + + config.bind("s", "echo hi") + config.test_all_command = "bin/test" + + assert_equal "echo hi", config.keys["s"] + assert_equal "bin/test", config.keys[""] + end + + def test_configure_yields_configuration + Testerobly.configure do |config| + config.test_command = "ruby %s" + end + + assert_equal "ruby %s", Testerobly.configuration.test_command + end + + def test_process_changes_enqueues_matching_tests + with_tmp_project do |dir| + write_file(dir, "lib/foo.rb", "# frozen_string_literal: true\n") + write_file(dir, "test/foo_test.rb", "# frozen_string_literal: true\n") + + main = build_main + + Dir.chdir(dir) do + main.process_changes(["lib/foo.rb"], []) + end + + queue = main.instance_variable_get(:@queue) + + assert_equal 1, queue.size + item = queue.pop + assert_equal "bin/test test/foo_test.rb", item[:command] + assert_equal "lib/foo.rb => test/foo_test.rb", item[:message] + end + end + + def test_process_changes_applies_on_change_hook + with_tmp_project do |dir| + write_file(dir, "lib/foo.rb", "# frozen_string_literal: true\n") + write_file(dir, "test/foo_test.rb", "# frozen_string_literal: true\n") + write_file(dir, "test/extra_test.rb", "# frozen_string_literal: true\n") + + Testerobly.configuration.on_change = Proc.new do |_path, tests| + tests << "test/extra_test.rb" + end + + main = build_main + + Dir.chdir(dir) do + main.process_changes(["lib/foo.rb"], []) + end + + queue = main.instance_variable_get(:@queue) + + assert_equal 1, queue.size + item = queue.pop + assert_equal "bin/test test/foo_test.rb test/extra_test.rb", item[:command] + end + end + + private + + def build_main + main = Testerobly::Main.allocate + main.instance_variable_set(:@queue, Thread::Queue.new) + main.instance_variable_set(:@pause_until, Time.now - 1) + main + end + + def with_tmp_project + Dir.mktmpdir do |dir| + FileUtils.mkdir_p(File.join(dir, "lib")) + FileUtils.mkdir_p(File.join(dir, "test")) + yield dir + end + end + + def write_file(base, relative_path, contents) + full_path = File.join(base, relative_path) + FileUtils.mkdir_p(File.dirname(full_path)) + File.write(full_path, contents) + end +end diff --git a/testerobly.gemspec b/testerobly.gemspec index 8f96b8b..6a258f4 100644 --- a/testerobly.gemspec +++ b/testerobly.gemspec @@ -2,7 +2,7 @@ Gem::Specification.new do |spec| spec.name = "testerobly" - spec.version = "1.1.1" + spec.version = "1.1.2" spec.summary = "Test runner to launch alongside your developer session for immediate feedback" spec.description = "" spec.authors = ["Robert Starsi"] @@ -14,4 +14,7 @@ Gem::Specification.new do |spec| spec.executables = %w[ testerobly ] spec.add_dependency "listen", "~> 3.9.0" + spec.add_dependency "logger", "~> 1.7" + + spec.add_development_dependency "minitest", "~> 5.0" end