Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing IO supression #634

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions script/test
Original file line number Diff line number Diff line change
Expand Up @@ -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 "$@"

6 changes: 3 additions & 3 deletions spec/integration/deploy_to_heroku_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions spec/integration/init_web_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
Expand All @@ -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"

Expand Down Expand Up @@ -59,31 +59,31 @@ 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
end
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
end
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
end
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"
Expand Down
36 changes: 34 additions & 2 deletions spec/support/should_run_successfully.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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