From 38db03b997de03eb795af4b1b0d5c3aeb0f8dd16 Mon Sep 17 00:00:00 2001 From: Agis Anastasopoulos Date: Wed, 22 Jul 2020 20:47:30 +0300 Subject: [PATCH] test: Add helper to temporarily suppress stdout This makes up testing output much cleaner. Part of #1 --- README.md | 6 ++++++ test/test_helpers.rb | 18 +++++++++++++++++- test/test_scheduling.rb | 14 +++++++------- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1729796..283582f 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,12 @@ Then you can execute the tests after spinning up a Redis instance at $ bundle exec rake ``` +To enable verbose output, set the `RSPECQ_DEBUG` environment variable: + +``` +$ RSPECQ_DEBUG=1 bundle exec rake +``` + ## License diff --git a/test/test_helpers.rb b/test/test_helpers.rb index 7b738e1..863e100 100644 --- a/test/test_helpers.rb +++ b/test/test_helpers.rb @@ -63,9 +63,25 @@ def start_worker(build_id:, worker_id: rand_id, suite:) Process.spawn( "#{EXEC_CMD} -w #{worker_id} -b #{build_id}", chdir: suite_path(suite), - out: (ENV["RSPECQ_DEBUG"] ? :out : "/dev/null"), + out: (ENV["RSPECQ_DEBUG"] ? :out : File::NULL), ) end + + # Supresses stdout of the code provided in the block + def silent + if ENV["RSPECQ_DEBUG"] + yield + return + end + + begin + orig = $stdout.clone + $stdout.reopen(File::NULL, 'w') + yield + ensure + $stdout.reopen(orig) + end + end end # To be subclassed from all test cases. diff --git a/test/test_scheduling.rb b/test/test_scheduling.rb index eb3c694..0a2f0ed 100644 --- a/test/test_scheduling.rb +++ b/test/test_scheduling.rb @@ -4,14 +4,14 @@ class TestScheduling < RSpecQTest def test_scheduling_with_timings_simple worker = new_worker("timings") worker.populate_timings = true - worker.work + silent { worker.work } assert_queue_well_formed(worker.queue) worker = new_worker("timings") # worker.populate_timings is false by default queue = worker.queue - worker.try_publish_queue!(queue) + silent { worker.try_publish_queue!(queue) } assert_equal [ "./test/sample_suites/timings/spec/very_slow_spec.rb", @@ -25,7 +25,7 @@ def test_scheduling_with_timings_simple def test_scheduling_with_timings_and_splitting worker = new_worker("scheduling") worker.populate_timings = true - worker.work + silent { worker.work } assert_queue_well_formed(worker.queue) @@ -33,7 +33,7 @@ def test_scheduling_with_timings_and_splitting worker = new_worker("scheduling") worker.populate_timings = true worker.file_split_threshold = 0.2 - worker.work + silent { worker.work } assert_queue_well_formed(worker.queue) @@ -47,7 +47,7 @@ def test_scheduling_with_timings_and_splitting worker = new_worker("scheduling") worker.populate_timings = true worker.file_split_threshold = 0.2 - worker.try_publish_queue!(worker.queue) + silent { worker.try_publish_queue!(worker.queue) } assert_equal [ "./test/sample_suites/scheduling/spec/foo_spec.rb[1:2:1]", @@ -59,14 +59,14 @@ def test_scheduling_with_timings_and_splitting def test_untimed_jobs_scheduled_in_the_middle worker = new_worker("scheduling_untimed/spec/foo") worker.populate_timings = true - worker.work + silent { worker.work } assert_queue_well_formed(worker.queue) assert worker.queue.build_successful? refute_empty worker.queue.timings worker = new_worker("scheduling_untimed") - worker.try_publish_queue!(worker.queue) + silent { worker.try_publish_queue!(worker.queue) } assert_equal [ "./test/sample_suites/scheduling_untimed/spec/foo/bar_spec.rb", "./test/sample_suites/scheduling_untimed/spec/foo/foo_spec.rb",