Class: Spoom::TestHelpers::Project

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/spoom/test_helpers/project.rb

Overview

A simple project abstraction for testing purposes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Project

Returns a new instance of Project.



22
23
24
25
26
# File 'lib/spoom/test_helpers/project.rb', line 22

def initialize(path)
  @path = path
  FileUtils.rm_rf(@path)
  FileUtils.mkdir_p(@path)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



18
19
20
# File 'lib/spoom/test_helpers/project.rb', line 18

def path
  @path
end

Instance Method Details

#bundle_exec(cmd, *args) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/spoom/test_helpers/project.rb', line 101

def bundle_exec(cmd, *args)
  opts = {}
  opts[:chdir] = path
  out, err, status = Open3.capture3(["bundle", "exec", cmd, *args].join(' '), opts)
  ExecResult.new(
    out: out,
    err: err,
    status: T.must(status.success?),
    exit_code: T.must(status.exitstatus)
  )
end

#bundle_installObject



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/spoom/test_helpers/project.rb', line 87

def bundle_install
  opts = {}
  opts[:chdir] = path
  out, err, status = Open3.capture3("bundle", "install", opts)
  ExecResult.new(
    out: out,
    err: err,
    status: T.must(status.success?),
    exit_code: T.must(status.exitstatus)
  )
end

#commit(message = "message", date: Time.now.utc) ⇒ Object



80
81
82
83
# File 'lib/spoom/test_helpers/project.rb', line 80

def commit(message = "message", date: Time.now.utc)
  Spoom::Git.exec("git add --all", path: path)
  Spoom::Git.exec("GIT_COMMITTER_DATE=\"#{date}\" git commit -m '#{message}' --date '#{date}'", path: path)
end

#create_and_checkout_branch(name) ⇒ Object



120
121
122
# File 'lib/spoom/test_helpers/project.rb', line 120

def create_and_checkout_branch(name)
  Spoom::Git.exec("git checkout -b #{name}", path: path)
end

#current_branchObject



125
126
127
# File 'lib/spoom/test_helpers/project.rb', line 125

def current_branch
  Spoom::Git.current_branch(path: path)
end

#destroyObject



115
116
117
# File 'lib/spoom/test_helpers/project.rb', line 115

def destroy
  FileUtils.rm_rf(path)
end

#filesObject



58
59
60
# File 'lib/spoom/test_helpers/project.rb', line 58

def files
  Dir.glob("#{@path}/**/*").sort
end

#gemfile(content) ⇒ Object



32
33
34
# File 'lib/spoom/test_helpers/project.rb', line 32

def gemfile(content)
  write("Gemfile", content)
end

#git_init(branch: "main") ⇒ Object



72
73
74
75
76
# File 'lib/spoom/test_helpers/project.rb', line 72

def git_init(branch: "main")
  Spoom::Git.exec("git init -q -b #{branch}", path: path)
  Spoom::Git.exec("git config user.name 'spoom-tests'", path: path)
  Spoom::Git.exec("git config user.email '[email protected]'", path: path)
end

#read(rel_path) ⇒ Object



64
65
66
# File 'lib/spoom/test_helpers/project.rb', line 64

def read(rel_path)
  File.read(absolute_path(rel_path))
end

#remove(rel_path) ⇒ Object



52
53
54
# File 'lib/spoom/test_helpers/project.rb', line 52

def remove(rel_path)
  FileUtils.rm_rf(absolute_path(rel_path))
end

#sorbet_config(content) ⇒ Object



38
39
40
# File 'lib/spoom/test_helpers/project.rb', line 38

def sorbet_config(content)
  write("sorbet/config", content)
end

#write(rel_path, content = "") ⇒ Object



44
45
46
47
48
# File 'lib/spoom/test_helpers/project.rb', line 44

def write(rel_path, content = "")
  path = absolute_path(rel_path)
  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, content)
end