Class BareTest::Persistence
In: lib/baretest/persistence.rb
Parent: Object

A simple file based storage. This is used to persist data between runs of baretest (caching data, keeping the last run‘s states for filtering, etc.) The data is stored in ~/.baretest, per project. A file with the name pattern

Methods

Attributes

project_dir  [R]  The directory of the project this Persistence instance is attached to
project_id  [R]  The id of the project this Persistence instance is attached to
storage_dir  [R]  The directory this Persistence instance stores its data

Public Class methods

BareTest uses a file of the form ’.baretest_id_*’ (where * is a 32 digits long hex) to uniquely identify a project. This ID is then used to associate stored data with the project.

[Source]

    # File lib/baretest/persistence.rb, line 32
32:     def self.determine_project_id(project_dir)
33:       found = Dir.glob("#{project_dir}/.baretest_id_*") { |path|
34:         break $1 if File.file?(path) && path =~ /id_([A-Fa-f0-9]{32})$/
35:       }
36:       unless found then
37:         found = UID.hex_uid
38:         File.open(".baretest_id_#{found}", "w") { |fh|
39:           # The content of this file is irrelevant, only its name. So lets
40:           # add a little bit of explaining text in case somebody wonders about
41:           # the purpose of this file.
42:           fh.write(
43:             "This file is used by baretest to find the persisted data in your ~/.baretest directory.\n" \
44:             "Deleting this file will result in orphaned persistence data.\n" \
45:             "See `baretest help reset`."
46:           )
47:         }
48:       end
49: 
50:       found
51:     end

Arguments:

project_dir:The directory of the project
storage_dir:The directory where this Persistence instance should store its data

[Source]

    # File lib/baretest/persistence.rb, line 66
66:     def initialize(project_dir=nil, storage_dir=nil)
67:       @storage_dir = File.expand_path(storage_dir || self.class.storage_path)
68:       @project_dir = File.expand_path(project_dir || ".")
69:       @project_id  = self.class.determine_project_id(@project_dir)
70:       stat         = File.stat(@project_dir)
71:       store('project', {
72:         :project_directory        => @project_dir,
73:         :project_directory_inode  => stat.ino,
74:         :project_directory_device => stat.dev
75:       })
76:     end

The default storage path base (~/.baretest)

[Source]

    # File lib/baretest/persistence.rb, line 25
25:     def self.storage_path
26:       File.expand_path('~/.baretest')
27:     end

Public Instance methods

Remove all files that store state, cache things etc.

[Source]

     # File lib/baretest/persistence.rb, line 137
137:     def clear
138:       delete('final_states')
139:     end

Deletes the file for the given filename.

filename:A relative path. Empty directories are recursively deleted up to (but without) Persistence#storage_dir. The path is relative to Persistence#storage_dir

[Source]

     # File lib/baretest/persistence.rb, line 118
118:     def delete(filename)
119:       raise "Invalid filename: #{filename}" if filename =~ %r{\A\.\./|/\.\./\z}
120:       project_storage_dir = "#{@storage_dir}/#{@project_id}"
121:       path                = "#{project_storage_dir}/#{filename}.yaml"
122: 
123:       File.delete(path)
124:       container = File.dirname(path)
125:       while container != project_storage_dir
126:         begin
127:           Dir.delete(container)
128:         rescue Errno::ENOTEMPTY
129:           break
130:         else
131:           container = File.dirname(container)
132:         end
133:       end
134:     end

Reads and deserializes the data in a given filename.

filename:A relative path. Directories are created on the fly if necessary. Must not be an absolute path. The path is relative to Persistence#storage_dir
default:The value to return in case the file does not exist. Alternatively you can pass a block that calculates the default.

[Source]

     # File lib/baretest/persistence.rb, line 101
101:     def read(filename, default=nil)
102:       raise "Invalid filename: #{filename}" if filename =~ %r{\A\.\./|/\.\./\z}
103:       path = "#{@storage_dir}/#{@project_id}/#{filename}.yaml"
104: 
105:       if File.exist?(path)
106:         YAML.load_file(path)
107:       elsif block_given?
108:         yield
109:       else
110:         default
111:       end
112:     end

Stores data to a file.

Arguments

filename:A relative path. Directories are created on the fly if necessary. Must not be an absolute path. The path is relative to Persistence#storage_dir
data:The data to store. Anything that can be serialized by YAML. This excludes IOs and Procs.

[Source]

    # File lib/baretest/persistence.rb, line 86
86:     def store(filename, data)
87:       raise "Invalid filename: #{filename}" unless filename =~ %r{\A[A-Za-z0-9_-][A-Za-z0-9_-]*\z}
88:       dir = "#{@storage_dir}/#{@project_id}"
89:       FileUtils.mkdir_p(dir)
90:       File.open("#{dir}/#{filename}.yaml", "w") do |fh|
91:         fh.write(data.to_yaml)
92:       end
93:     end

[Validate]