Class: TurboTests::Shim

Inherits:
Object
  • Object
show all
Defined in:
lib/turbo_tests/shim.rb

Defined Under Namespace

Classes: Result

Constant Summary collapse

DEFAULT_RELATIVE_PATH =
File.join("bin", "turbo_tests")
MANAGED_MARKER =
"Generated by turbo_tests2 shim install"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_root: Dir.pwd, path: nil) ⇒ Shim

Returns a new instance of Shim.



22
23
24
25
# File 'lib/turbo_tests/shim.rb', line 22

def initialize(project_root: Dir.pwd, path: nil)
  @project_root = File.expand_path(project_root)
  @path = File.expand_path(path || DEFAULT_RELATIVE_PATH, @project_root)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



20
21
22
# File 'lib/turbo_tests/shim.rb', line 20

def path
  @path
end

#project_rootObject (readonly)

Returns the value of attribute project_root.



20
21
22
# File 'lib/turbo_tests/shim.rb', line 20

def project_root
  @project_root
end

Class Method Details

.install(project_root: Dir.pwd, path: nil) ⇒ Object



11
12
13
# File 'lib/turbo_tests/shim.rb', line 11

def install(project_root: Dir.pwd, path: nil)
  new(project_root: project_root, path: path).install
end

.remove(project_root: Dir.pwd, path: nil) ⇒ Object



15
16
17
# File 'lib/turbo_tests/shim.rb', line 15

def remove(project_root: Dir.pwd, path: nil)
  new(project_root: project_root, path: path).remove
end

Instance Method Details

#installObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/turbo_tests/shim.rb', line 27

def install
  existing_managed = false
  if File.exist?(path)
    existing = File.read(path)
    return result(:unchanged, "Shim already installed at #{display_path}.", 0) if existing == rendered_content
    return result(:conflict, "Refusing to overwrite unmanaged file at #{display_path}.", 1) unless managed_content?(existing)
    existing_managed = true
  end

  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, rendered_content)
  FileUtils.chmod(0o755, path)

  result(existing_managed ? :updated : :installed, "Installed turbo_tests shim at #{display_path}.", 0)
end

#removeObject



43
44
45
46
47
48
49
50
51
# File 'lib/turbo_tests/shim.rb', line 43

def remove
  return result(:missing, "No shim found at #{display_path}.", 0) unless File.exist?(path)

  content = File.read(path)
  return result(:conflict, "Refusing to remove unmanaged file at #{display_path}.", 1) unless managed_content?(content)

  File.delete(path)
  result(:removed, "Removed turbo_tests shim at #{display_path}.", 0)
end