11require "fileutils"
22require "tmpdir"
3+ require "stringio"
4+ require "zlib"
35
46require "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 ( /\A Uploading 21\. 00 B \( including data\. sqlite\) \. \. \. \n \z / ) . to_stdout
106+ . to output ( /\A Uploading 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 ( /\A Uploading 10\. 00 B\. \. \. \n \z / ) . to_stdout
121+ . to output ( /\A Uploading 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