diff --git a/script/test b/script/test index 363859c5..288d1ec0 100755 --- a/script/test +++ b/script/test @@ -6,24 +6,30 @@ set -o pipefail source ./script/override_shards -printf "\nChecking code formatting...\n\n" +function check_print { + if [ -t 0 ]; then + printf "$1" + fi +} + +check_print "\nChecking code formatting...\n\n" if ! crystal tool format --check src spec > /dev/null; then - printf "\nCode is not formatted.\n" - printf "\nFormat the code with: crystal tool format src spec\n\n" + check_print "\nCode is not formatted.\n" + check_print "\nFormat the code with: crystal tool format src spec\n\n" exit 1 else - printf "\nCrystal format checks passed.\n\n" + check_print "\nCrystal format checks passed.\n\n" fi if ! crystal ./bin/ameba.cr; then - printf "\nCode did not pass Ameba checks.\n" - printf "\nResolve Ameba linter errors, then run this checker again\n\n" + check_print "\nCode did not pass Ameba checks.\n" + check_print "\nResolve Ameba linter errors, then run this checker again\n\n" exit 1 else - printf "\nAmeba linter checks passed.\n\n" + check_print "\nAmeba linter checks passed.\n\n" fi -printf "\nRunning specs with 'crystal spec'\n\n" +check_print "\nRunning specs with 'crystal spec'\n\n" crystal spec "$@" diff --git a/spec/integration/deploy_to_heroku_spec.cr b/spec/integration/deploy_to_heroku_spec.cr index 045211c1..98206200 100644 --- a/spec/integration/deploy_to_heroku_spec.cr +++ b/spec/integration/deploy_to_heroku_spec.cr @@ -9,7 +9,7 @@ require "../spec_helper" describe "Initializing a new web project" do it "deploys a full web app successfully" do - puts "Web app: Running Heroku deployment. This will take awhile...".colorize(:yellow) + test_puts "Web app: Running Heroku deployment. This will take awhile...".colorize(:yellow) should_run_successfully "crystal src/lucky.cr init.custom test-project" app = generate_heroku_app_name @@ -24,7 +24,7 @@ require "../spec_helper" end it "deploys an API app successfully" do - puts "API app: Running Heroku deployment. This will take awhile...".colorize(:yellow) + test_puts "API app: Running Heroku deployment. This will take awhile...".colorize(:yellow) should_run_successfully "crystal src/lucky.cr init.custom test-project -- --api" app = generate_heroku_app_name @@ -45,7 +45,7 @@ require "../spec_helper" private def deploy_to_heroku(app_name, block = nil) Dir.cd("./test-project") do - puts "Deploying #{app_name}" + test_puts "Deploying #{app_name}" should_run_successfully "yarn install" io = IO::Memory.new should_run_successfully("heroku apps", output: io) diff --git a/spec/integration/init_web_spec.cr b/spec/integration/init_web_spec.cr index da4aab9a..8f433505 100644 --- a/spec/integration/init_web_spec.cr +++ b/spec/integration/init_web_spec.cr @@ -4,7 +4,7 @@ include ShouldRunSuccessfully describe "Initializing a new web project" do it "creates a full web app successfully" do - puts "Web app: Running integration spec. This might take awhile...".colorize(:yellow) + test_io.puts "Web app: Running integration spec. This might take awhile...".colorize(:yellow) with_project_cleanup do should_run_successfully "crystal run src/lucky.cr -- init.custom test-project" FileUtils.cp("spec/support/cat.gif", "test-project/public/assets/images/") @@ -26,7 +26,7 @@ describe "Initializing a new web project" do end it "creates a full web app with generator" do - puts "Web app generators: Running integration spec. This might take awhile...".colorize(:yellow) + test_io.puts "Web app generators: Running integration spec. This might take awhile...".colorize(:yellow) with_project_cleanup do should_run_successfully "crystal run src/lucky.cr -- init.custom test-project --no-auth" @@ -59,7 +59,7 @@ describe "Initializing a new web project" do end it "creates an api only web app successfully" do - puts "Api only: Running integration spec. This might take awhile...".colorize(:yellow) + test_io.puts "Api only: Running integration spec. This might take awhile...".colorize(:yellow) with_project_cleanup do should_run_successfully "crystal run src/lucky.cr -- init.custom test-project --api" compile_and_run_specs_on_test_project @@ -67,7 +67,7 @@ describe "Initializing a new web project" do end it "creates an api only app without auth" do - puts "Api only without auth: Running integration spec. This might take awhile...".colorize(:yellow) + test_io.puts "Api only without auth: Running integration spec. This might take awhile...".colorize(:yellow) with_project_cleanup do should_run_successfully "crystal run src/lucky.cr -- init.custom test-project --api --no-auth" compile_and_run_specs_on_test_project @@ -75,7 +75,7 @@ describe "Initializing a new web project" do end it "creates a full app without auth" do - puts "Web app without auth: Running integration spec. This might take awhile...".colorize(:yellow) + test_io.puts "Web app without auth: Running integration spec. This might take awhile...".colorize(:yellow) with_project_cleanup do should_run_successfully "crystal run src/lucky.cr -- init.custom test-project --no-auth" compile_and_run_specs_on_test_project @@ -83,7 +83,7 @@ describe "Initializing a new web project" do end it "creates a full app in a different directory" do - puts "Web app with custom directory: Running integration spec.".colorize(:yellow) + test_io.puts "Web app with custom directory: Running integration spec.".colorize(:yellow) with_project_cleanup(project_directory: "/tmp/home/bob/test-project", skip_db_drop: true) do FileUtils.mkdir_p "/tmp/home/bob" should_run_successfully "crystal run src/lucky.cr -- init.custom test-project --dir /tmp/home/bob" diff --git a/spec/support/should_run_successfully.cr b/spec/support/should_run_successfully.cr index 4c0b8571..494cc264 100644 --- a/spec/support/should_run_successfully.cr +++ b/spec/support/should_run_successfully.cr @@ -4,8 +4,8 @@ module ShouldRunSuccessfully command, shell: true, env: ENV.to_h, - output: STDOUT, - error: STDERR + output: input_io, + error: output_io ) result.exit_status.should be_successful @@ -14,4 +14,36 @@ module ShouldRunSuccessfully private def be_successful eq 0 end + + private def input_io + STDIN + end + + private def output_io + OutputIO.instance.io + end + + private def test_io + if Log.for("*").level == Log::Severity::Debug + STDERR + else + File.open(File::NULL, "w") + end + end + + private def test_puts(*args) + test_io.puts args + end +end + +class OutputIO + getter io + + def initialize + @io = IO::Memory.new + end + + def self.instance + @@instance ||= new + end end