Module: RSpec::RemoteFixtures

Defined in:
lib/rspec/remote_fixtures.rb,
lib/rspec/remote_fixtures/config.rb,
lib/rspec/remote_fixtures/backend.rb,
lib/rspec/remote_fixtures/version.rb,
lib/rspec/remote_fixtures/manifest.rb,
lib/rspec/remote_fixtures/example_group.rb,
lib/rspec/remote_fixtures/backend/s3_backend.rb

Overview

An on-demand-fixture-downloader for RSpec

Defined Under Namespace

Modules: Backend, Config, ExampleGroup, Manifest, World Classes: Error

Constant Summary collapse

VERSION =
'0.2.1'

Class Method Summary collapse

Class Method Details

.backend_instObject



25
26
27
# File 'lib/rspec/remote_fixtures.rb', line 25

def self.backend_inst
  @backend_inst ||= Config.backend.new
end

.ensure_file(relative_path) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rspec/remote_fixtures.rb', line 29

def self.ensure_file(relative_path)
  full_path = full_path_from_relative(relative_path)
  entry = Manifest.entry_for(relative_path)
  log_not_found(relative_path) unless entry

  verify_checksum(entry, full_path) if Config.check_remote_fixture_digest == :always
  file_present = File.exist?(full_path)

  retrieve_entry(entry, full_path) unless file_present
  maybe_verify(entry, full_path, file_present)

  full_path
end

.full_path_from_relative(relative_path) ⇒ Object



43
44
45
# File 'lib/rspec/remote_fixtures.rb', line 43

def self.full_path_from_relative(relative_path)
  Pathname.new(Config.fixture_path).join(relative_path)
end

.log_not_found(relative_path) ⇒ Object



72
73
74
75
# File 'lib/rspec/remote_fixtures.rb', line 72

def self.log_not_found(relative_path)
  msg = "Warning: fixture #{relative_path} not found in manifest, this spec may fail elsewhere!"
  report(msg)
end

.maybe_verify(entry, path, file_present) ⇒ Object

Raises:



54
55
56
57
58
59
60
61
62
63
# File 'lib/rspec/remote_fixtures.rb', line 54

def self.maybe_verify(entry, path, file_present)
  config_val = Config.check_remote_fixture_digest
  return if config_val == :never
  return if config_val == :download && file_present

  raise Error, "Unable to verify digest for #{path} - entry not found" unless entry

  digest = Manifest.compute_digest(path)
  raise Error, "Digest for #{path} did not match manifest entry, aborting!" unless digest == entry['digest']
end

.report(msg) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/rspec/remote_fixtures.rb', line 82

def self.report(msg)
  if (defined? ::RSpec) && ::RSpec.respond_to?(:configuration)
    ::RSpec.configuration.reporter.message(msg)
  else
    puts msg
  end
end

.retrieve_entry(entry, dest) ⇒ Object

Raises:



47
48
49
50
51
52
# File 'lib/rspec/remote_fixtures.rb', line 47

def self.retrieve_entry(entry, dest)
  raise Error, "Attempted to retrieve #{dest} but it wasn't present in the manifest" unless entry

  FileUtils.mkdir_p(dest.dirname)
  backend_inst.download(entry['remote_path'], dest)
end

.setup_examples!Object



65
66
67
68
69
70
# File 'lib/rspec/remote_fixtures.rb', line 65

def self.setup_examples!
  return if @setup_done

  @setup_done = true
  RSpec.configuration.prepend(ExampleGroup)
end

.setup_rspec!Object



90
91
92
93
94
95
96
97
98
# File 'lib/rspec/remote_fixtures.rb', line 90

def self.setup_rspec!
  RSpec.configuration.fixture_path = Config.fixture_path if RSpec.configuration.respond_to?(:fixture_path=)
  if RSpec.configuration.respond_to?(:file_fixture_path=)
    RSpec.configuration.file_fixture_path = Config.fixture_path
  end

  RSpec::Core::World.prepend(World)
  FactoryBot::SyntaxRunner.include(ExampleGroup) if defined? FactoryBot::SyntaxRunner
end

.upload(relative_path, digest) ⇒ Object



77
78
79
80
# File 'lib/rspec/remote_fixtures.rb', line 77

def self.upload(relative_path, digest)
  full_path = full_path_from_relative(relative_path)
  backend_inst.upload(full_path, digest)
end