Class: Insta::SnapshotFile

Inherits:
Object
  • Object
show all
Defined in:
lib/insta/snapshot_file.rb,
sig/insta/snapshot_file.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_path, test_class, test_name, extension = nil) ⇒ SnapshotFile

: (String, String, String, ?String?) -> void

Parameters:

  • (String)
  • (String)
  • (String)
  • (String, nil)


12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/insta/snapshot_file.rb', line 12

def initialize(base_path, test_class, test_name, extension = nil)
  directory_resolver = Insta.configuration.snapshot_directory
  directory = if directory_resolver
                directory_resolver.call(test_class: test_class)
              else
                SnapshotName.underscore(test_class)
              end
  @base_path = Pathname.new(base_path) / directory
  @test_name = test_name
  @extension = extension || Insta.configuration.snapshot_extension
  @counter = 0
end

Instance Attribute Details

#base_pathPathname (readonly)

: Pathname

Returns:

  • (Pathname)


8
9
10
# File 'lib/insta/snapshot_file.rb', line 8

def base_path
  @base_path
end

#extensionString (readonly)

: String

Returns:

  • (String)


9
10
11
# File 'lib/insta/snapshot_file.rb', line 9

def extension
  @extension
end

Instance Method Details

#named_path(name) ⇒ Pathname

: (String) -> Pathname

Parameters:

  • (String)

Returns:

  • (Pathname)


40
41
42
# File 'lib/insta/snapshot_file.rb', line 40

def named_path(name)
  @base_path / "#{SnapshotName.sanitize(name)}#{@extension}"
end

#path_for(options = {}) ⇒ Pathname

: (?Hash[Symbol, untyped]) -> Pathname

Parameters:

  • (Hash[Symbol, untyped])

Returns:

  • (Pathname)


26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/insta/snapshot_file.rb', line 26

def path_for(options = {})
  @counter += 1
  filename_resolver = Insta.configuration.snapshot_filename

  derived = if filename_resolver
              filename_resolver.call(test_name: @test_name, counter: @counter, options: options)
            else
              SnapshotName.derive(@test_name, counter: @counter, options: options)
            end

  @base_path / "#{derived}#{@extension}"
end

#pending_path(path) ⇒ Pathname

: (Pathname) -> Pathname

Parameters:

  • (Pathname)

Returns:

  • (Pathname)


45
46
47
# File 'lib/insta/snapshot_file.rb', line 45

def pending_path(path)
  Pathname.new("#{path}.new")
end

#read(path) ⇒ Snapshot?

: (Pathname) -> Snapshot?

Parameters:

  • (Pathname)

Returns:



58
59
60
61
62
# File 'lib/insta/snapshot_file.rb', line 58

def read(path)
  return nil unless path.exist?

  Snapshot.parse(path.read)
end

#write(path, content, metadata = {}) ⇒ void

This method returns an undefined value.

: (Pathname, String, Snapshot::snapshot_metadata) -> void

Parameters:

  • (Pathname)
  • (String)
  • (Snapshot::snapshot_metadata)


50
51
52
53
54
55
# File 'lib/insta/snapshot_file.rb', line 50

def write(path, content,  = {})
  FileUtils.mkdir_p(path.dirname)
  snapshot = Snapshot.new(content, )

  path.write(snapshot.serialize)
end