/ spec / spec_helper.cr
spec_helper.cr
 1  require "spec"
 2  require "http/server"
 3  require "file_utils"
 4  require "webmock"
 5  
 6  require "../src/barista"
 7  require "./support/**"
 8  
 9  include WithHelpers
10  
11  WebMock.allow_net_connect = true
12  
13  def cache_callbacks
14    callbacks = Barista::Behaviors::Omnibus::CacheCallbacks.new
15    callbacks.fetch do |cacher|
16      Support::Cacher.fetch_cache(cacher)
17    end
18  
19    callbacks.update do |info, s|
20      Support::Cacher.update_cache(info, s)
21    end
22  
23    callbacks
24  end
25  
26  def fixture_url
27    "http://localhost:3003"
28  end
29  
30  def fixtures_path
31    Path["#{__DIR__}/support/fixtures"].expand.to_s
32  end
33  
34  def downloads_path
35    Path["#{__DIR__}/support/downloads"].expand.to_s
36  end
37  
38  def cache_path
39    File.join(fixtures_path, "files", "cache")
40  end
41  
42  def external_fixture(script)
43    "#{__DIR__}/../fixtures/#{script}"
44  end
45  
46  def barista_test_user
47    ENV["BARISTA_TEST_USER"]?
48  end
49  
50  def with_webmock
51    WebMock.allow_net_connect = false
52    yield
53  ensure
54    WebMock.reset
55    WebMock.allow_net_connect = true
56  end
57  
58  def reset_paths
59    FileUtils.mkdir_p(cache_path)
60    FileUtils.mkdir_p(downloads_path)
61  
62    Dir.cd(downloads_path) do
63      FileUtils.rm_rf(Dir.children("."))
64    end
65    
66    Dir.cd(cache_path) do
67      FileUtils.rm_rf(Dir.children("."))
68    end
69  end
70  
71  def mkdir(path : String, parents : Bool = true)
72    parents ? FileUtils.mkdir_p(path) : FileUtils.mkdir(path)
73  end
74  
75  Barista::Log.level = Log::Severity::Debug
76  
77  server = HTTP::Server.new([
78    Support::CacheHandler.new(fixtures_path),  
79    HTTP::StaticFileHandler.new("#{fixtures_path}/files")
80  ])
81  
82  Spec.before_each do
83    reset_paths unless ENV["CLEAN"]? == "false"
84  end
85  
86  Spec.after_each do
87    reset_paths unless ENV["CLEAN"]? == "false"
88  end
89  
90  Spec.after_suite do
91    server.try(&.close)
92  end
93  
94  spawn do
95    server.listen(3003)
96  end