Skip to content

Commit ca6c5dc

Browse files
authored
Merge pull request #27 from openaustralia/feature/7-warn-when-scraper-produces-no-output
Tell the user when a scraper runs but outputs nothing
2 parents 9b6ba2b + 3dfbb07 commit ca6c5dc

3 files changed

Lines changed: 74 additions & 6 deletions

File tree

‎.rubocop_todo.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Metrics/MethodLength:
3636
# Offense count: 1
3737
# Configuration parameters: CountComments, CountAsOne.
3838
Metrics/ModuleLength:
39-
Max: 114
39+
Max: 124
4040

4141
# Offense count: 1
4242
# Configuration parameters: ExpectMatchingDefinition, CheckDefinitionPathHierarchy, CheckDefinitionPathHierarchyRoots, Regex, IgnoreExecutableScripts, AllowedAcronyms.

‎lib/morph-cli.rb‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,23 @@ def self.execute(directory, _development, env_config, skip_data: false)
3131

3232
file = MorphCLI.create_tar(directory, all_paths)
3333

34+
scraper_output = run(file, env_config)
35+
36+
puts "Scraper didn't output anything, but it ran successfully." unless scraper_output
37+
end
38+
39+
# Uploads the code to the server, streams the run output to the local
40+
# stdout/stderr and returns whether the scraper itself wrote anything
41+
# to stdout or stderr
42+
def self.run(file, env_config)
3443
connection = Faraday.new(url: env_config[:base_url]) do |f|
3544
f.request :multipart
3645
f.response :raise_error
3746
f.adapter Faraday.default_adapter
3847
end
3948

4049
buffer = +""
50+
scraper_output = false
4151
connection.post("/run") do |req|
4252
req.body = {
4353
api_key: env_config[:api_key],
@@ -52,12 +62,18 @@ def self.execute(directory, _development, env_config, skip_data: false)
5262

5363
before, match, after = chunk.rpartition("\n")
5464
buffer << before << match
55-
buffer.split("\n").each { |l| log(l) }
65+
buffer.split("\n").each do |l|
66+
stream = log(l)
67+
scraper_output = true if %w[stdout stderr].include?(stream)
68+
end
5669
buffer = after
5770
end
5871
end
72+
scraper_output
5973
end
6074

75+
# Writes the line to the local stdout/stderr and returns the name of the
76+
# stream it came from
6177
def self.log(line)
6278
return if line.empty?
6379

@@ -72,6 +88,7 @@ def self.log(line)
7288
end
7389

7490
s.puts a["text"]
91+
a["stream"]
7592
end
7693

7794
def self.config_path

‎spec/morph_cli_spec.rb‎

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
require "fileutils"
22
require "tmpdir"
3+
require "stringio"
4+
require "zlib"
35

46
require "spec_helper"
57

@@ -20,6 +22,23 @@ def with_scraper_directory
2022
end
2123
end
2224

25+
# Extracts tar entry names from a multipart request body that contains
26+
# a gzip-compressed tar as one of its parts.
27+
def tar_entry_names(body)
28+
binary = body.b
29+
gzip_start = binary.index("\x1f\x8b".b)
30+
return [] unless gzip_start
31+
32+
gzip_io = StringIO.new(binary[gzip_start..])
33+
names = []
34+
Zlib::GzipReader.wrap(gzip_io) do |gz|
35+
Minitar::Input.open(gz) { |tar| tar.each { |entry| names << entry.full_name } }
36+
end
37+
names
38+
rescue StandardError
39+
[]
40+
end
41+
2342
it "uploads the scraper and streams the run output to stdout" do
2443
stub_request(:post, "https://morph.io/run")
2544
.to_return(status: 200, body: %({"stream":"stdout","text":"hello from morph"}\n))
@@ -30,6 +49,38 @@ def with_scraper_directory
3049
end
3150
end
3251

52+
it "tells the user the run succeeded when the scraper produces no output" do
53+
stub_request(:post, "https://morph.io/run")
54+
.to_return(status: 200, body: %({"stream":"internalout","text":"Injecting configuration"}\n))
55+
56+
with_scraper_directory do |dir|
57+
expect { described_class.execute(dir, false, env_config) }
58+
.to output(/Scraper didn't output anything, but it ran successfully\./).to_stdout
59+
end
60+
end
61+
62+
it "doesn't add a message when the scraper writes to stdout" do
63+
stub_request(:post, "https://morph.io/run")
64+
.to_return(status: 200, body: %({"stream":"stdout","text":"hello from morph"}\n))
65+
66+
with_scraper_directory do |dir|
67+
expect { described_class.execute(dir, false, env_config) }
68+
.not_to output(/ran successfully/).to_stdout
69+
end
70+
end
71+
72+
it "doesn't add a message when the scraper writes to stderr" do
73+
stub_request(:post, "https://morph.io/run")
74+
.to_return(status: 200, body: %({"stream":"stderr","text":"oops"}\n))
75+
76+
with_scraper_directory do |dir|
77+
expect do
78+
expect { described_class.execute(dir, false, env_config) }
79+
.not_to output(/ran successfully/).to_stdout
80+
end.to output("oops\n").to_stderr
81+
end
82+
end
83+
3384
it "posts the API key and the gzipped code as multipart form data" do
3485
stub_request(:post, "https://morph.io/run").to_return(status: 200, body: "")
3586

@@ -52,11 +103,11 @@ def with_scraper_directory
52103
File.write(File.join(dir, "data.sqlite"), "sqlite data")
53104

54105
expect { described_class.execute(dir, false, env_config) }
55-
.to output(/\AUploading 21\.00 B \(including data\.sqlite\)\.\.\.\n\z/).to_stdout
106+
.to output(/\AUploading 21\.00 B \(including data\.sqlite\)\.\.\.\n/).to_stdout
56107
end
57108

58109
expect(WebMock).to(have_requested(:post, "https://morph.io/run").with do |req|
59-
req.body.include?("data.sqlite")
110+
tar_entry_names(req.body).include?("data.sqlite")
60111
end)
61112
end
62113

@@ -67,11 +118,11 @@ def with_scraper_directory
67118
File.write(File.join(dir, "data.sqlite"), "sqlite data")
68119

69120
expect { described_class.execute(dir, false, env_config, skip_data: true) }
70-
.to output(/\AUploading 10\.00 B\.\.\.\n\z/).to_stdout
121+
.to output(/\AUploading 10\.00 B\.\.\.\n/).to_stdout
71122
end
72123

73124
expect(WebMock).to(have_requested(:post, "https://morph.io/run").with do |req|
74-
!req.body.include?("data.sqlite")
125+
!tar_entry_names(req.body).include?("data.sqlite")
75126
end)
76127
end
77128

0 commit comments

Comments
 (0)