Skip to content

Commit e582fd0

Browse files
committed
Merge branch 'io_testing' of github.com:wontruefree/lucky_cli into io_testing
2 parents 6d247a4 + b98030d commit e582fd0

File tree

4 files changed

+57
-19
lines changed

4 files changed

+57
-19
lines changed

script/test

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,30 @@ set -o pipefail
66

77
source ./script/override_shards
88

9-
printf "\nChecking code formatting...\n\n"
9+
function check_print {
10+
if [ -t 0 ]; then
11+
printf "$1"
12+
fi
13+
}
14+
15+
check_print "\nChecking code formatting...\n\n"
1016

1117
if ! crystal tool format --check src spec > /dev/null; then
12-
printf "\nCode is not formatted.\n"
13-
printf "\nFormat the code with: crystal tool format src spec\n\n"
18+
check_print "\nCode is not formatted.\n"
19+
check_print "\nFormat the code with: crystal tool format src spec\n\n"
1420
exit 1
1521
else
16-
printf "\nCrystal format checks passed.\n\n"
22+
check_print "\nCrystal format checks passed.\n\n"
1723
fi
1824

1925
if ! crystal ./bin/ameba.cr; then
20-
printf "\nCode did not pass Ameba checks.\n"
21-
printf "\nResolve Ameba linter errors, then run this checker again\n\n"
26+
check_print "\nCode did not pass Ameba checks.\n"
27+
check_print "\nResolve Ameba linter errors, then run this checker again\n\n"
2228
exit 1
2329
else
24-
printf "\nAmeba linter checks passed.\n\n"
30+
check_print "\nAmeba linter checks passed.\n\n"
2531
fi
2632

27-
printf "\nRunning specs with 'crystal spec'\n\n"
33+
check_print "\nRunning specs with 'crystal spec'\n\n"
2834
crystal spec "$@"
2935

spec/integration/deploy_to_heroku_spec.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require "../spec_helper"
99

1010
describe "Initializing a new web project" do
1111
it "deploys a full web app successfully" do
12-
puts "Web app: Running Heroku deployment. This will take awhile...".colorize(:yellow)
12+
test_puts "Web app: Running Heroku deployment. This will take awhile...".colorize(:yellow)
1313
should_run_successfully "crystal src/lucky.cr init.custom test-project"
1414
app = generate_heroku_app_name
1515

@@ -24,7 +24,7 @@ require "../spec_helper"
2424
end
2525

2626
it "deploys an API app successfully" do
27-
puts "API app: Running Heroku deployment. This will take awhile...".colorize(:yellow)
27+
test_puts "API app: Running Heroku deployment. This will take awhile...".colorize(:yellow)
2828
should_run_successfully "crystal src/lucky.cr init.custom test-project -- --api"
2929
app = generate_heroku_app_name
3030

@@ -45,7 +45,7 @@ require "../spec_helper"
4545

4646
private def deploy_to_heroku(app_name, block = nil)
4747
Dir.cd("./test-project") do
48-
puts "Deploying #{app_name}"
48+
test_puts "Deploying #{app_name}"
4949
should_run_successfully "yarn install"
5050
io = IO::Memory.new
5151
should_run_successfully("heroku apps", output: io)

spec/integration/init_web_spec.cr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ include ShouldRunSuccessfully
44

55
describe "Initializing a new web project" do
66
it "creates a full web app successfully" do
7-
puts "Web app: Running integration spec. This might take awhile...".colorize(:yellow)
7+
test_io.puts "Web app: Running integration spec. This might take awhile...".colorize(:yellow)
88
with_project_cleanup do
99
should_run_successfully "crystal run src/lucky.cr -- init.custom test-project"
1010
FileUtils.cp("spec/support/cat.gif", "test-project/public/assets/images/")
@@ -26,7 +26,7 @@ describe "Initializing a new web project" do
2626
end
2727

2828
it "creates a full web app with generator" do
29-
puts "Web app generators: Running integration spec. This might take awhile...".colorize(:yellow)
29+
test_io.puts "Web app generators: Running integration spec. This might take awhile...".colorize(:yellow)
3030
with_project_cleanup do
3131
should_run_successfully "crystal run src/lucky.cr -- init.custom test-project --no-auth"
3232

@@ -59,31 +59,31 @@ describe "Initializing a new web project" do
5959
end
6060

6161
it "creates an api only web app successfully" do
62-
puts "Api only: Running integration spec. This might take awhile...".colorize(:yellow)
62+
test_io.puts "Api only: Running integration spec. This might take awhile...".colorize(:yellow)
6363
with_project_cleanup do
6464
should_run_successfully "crystal run src/lucky.cr -- init.custom test-project --api"
6565
compile_and_run_specs_on_test_project
6666
end
6767
end
6868

6969
it "creates an api only app without auth" do
70-
puts "Api only without auth: Running integration spec. This might take awhile...".colorize(:yellow)
70+
test_io.puts "Api only without auth: Running integration spec. This might take awhile...".colorize(:yellow)
7171
with_project_cleanup do
7272
should_run_successfully "crystal run src/lucky.cr -- init.custom test-project --api --no-auth"
7373
compile_and_run_specs_on_test_project
7474
end
7575
end
7676

7777
it "creates a full app without auth" do
78-
puts "Web app without auth: Running integration spec. This might take awhile...".colorize(:yellow)
78+
test_io.puts "Web app without auth: Running integration spec. This might take awhile...".colorize(:yellow)
7979
with_project_cleanup do
8080
should_run_successfully "crystal run src/lucky.cr -- init.custom test-project --no-auth"
8181
compile_and_run_specs_on_test_project
8282
end
8383
end
8484

8585
it "creates a full app in a different directory" do
86-
puts "Web app with custom directory: Running integration spec.".colorize(:yellow)
86+
test_io.puts "Web app with custom directory: Running integration spec.".colorize(:yellow)
8787
with_project_cleanup(project_directory: "/tmp/home/bob/test-project", skip_db_drop: true) do
8888
FileUtils.mkdir_p "/tmp/home/bob"
8989
should_run_successfully "crystal run src/lucky.cr -- init.custom test-project --dir /tmp/home/bob"

spec/support/should_run_successfully.cr

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ module ShouldRunSuccessfully
44
command,
55
shell: true,
66
env: ENV.to_h,
7-
output: STDOUT,
8-
error: STDERR
7+
output: input_io,
8+
error: output_io
99
)
1010

1111
result.exit_status.should be_successful
@@ -14,4 +14,36 @@ module ShouldRunSuccessfully
1414
private def be_successful
1515
eq 0
1616
end
17+
18+
private def input_io
19+
STDIN
20+
end
21+
22+
private def output_io
23+
OutputIO.instance.io
24+
end
25+
26+
private def test_io
27+
if Log.for("*").level == Log::Severity::Debug
28+
STDERR
29+
else
30+
File.open(File::NULL, "w")
31+
end
32+
end
33+
34+
private def test_puts(*args)
35+
test_io.puts args
36+
end
37+
end
38+
39+
class OutputIO
40+
getter io
41+
42+
def initialize
43+
@io = IO::Memory.new
44+
end
45+
46+
def self.instance
47+
@@instance ||= new
48+
end
1749
end

0 commit comments

Comments
 (0)