Class: TestRunner::RepositorySource

Inherits:
Object
  • Object
show all
Defined in:
lib/test-runner/repository_source.rb

Constant Summary collapse

ROOT_DIRECTORY =
'os-test-runner-repository-sources'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original_path:, root_directory: nil, nixpkgs_path: nil, nix_system: nil) ⇒ RepositorySource

Returns a new instance of RepositorySource.



17
18
19
20
21
22
23
# File 'lib/test-runner/repository_source.rb', line 17

def initialize(original_path:, root_directory: nil, nixpkgs_path: nil, nix_system: nil)
  @original_path = File.expand_path(original_path)
  @root_directory = File.expand_path(root_directory || default_root_directory)
  @nixpkgs_path = File.expand_path(nixpkgs_path || ENV.fetch('TEST_RUNNER_NIXPKGS_PATH'))
  @nix_system = nix_system || ENV.fetch('TEST_RUNNER_NIX_SYSTEM')
  @path = nil
end

Instance Attribute Details

#original_path ⇒ Object (readonly)

Returns the value of attribute original_path.



10
11
12
# File 'lib/test-runner/repository_source.rb', line 10

def original_path
  @original_path
end

#path ⇒ Object (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/test-runner/repository_source.rb', line 10

def path
  @path
end

#root_directory ⇒ Object (readonly)

Returns the value of attribute root_directory.



10
11
12
# File 'lib/test-runner/repository_source.rb', line 10

def root_directory
  @root_directory
end

Class Method Details

.open(original_path:, root_directory: nil, nixpkgs_path: nil, nix_system: nil) ⇒ Object



12
13
14
15
# File 'lib/test-runner/repository_source.rb', line 12

def self.open(original_path:, root_directory: nil, nixpkgs_path: nil, nix_system: nil)
  source = new(original_path:, root_directory:, nixpkgs_path:, nix_system:)
  source.open { yield(source) }
end

Instance Method Details

#default_root_directory ⇒ Object (protected)



75
76
77
78
79
80
# File 'lib/test-runner/repository_source.rb', line 75

def default_root_directory
  runtime_dir = ENV.fetch('XDG_RUNTIME_DIR', nil)
  runtime_dir = Dir.tmpdir if runtime_dir.nil? || runtime_dir.empty?

  File.join(runtime_dir, "#{ROOT_DIRECTORY}-#{Process.uid}")
end

#helper_file ⇒ Object (protected)



44
45
46
# File 'lib/test-runner/repository_source.rb', line 44

def helper_file
  File.expand_path('../../nix/resolve-repository-source.nix', __dir__)
end

#open ⇒ Object



25
26
27
28
29
30
# File 'lib/test-runner/repository_source.rb', line 25

def open
  GcRoot.new(root_directory:).open do |root_path|
    @path = resolve_and_root_source(root_path)
    yield(self)
  end
end

#resolve_and_root_source(root_path) ⇒ Object (protected)



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/test-runner/repository_source.rb', line 48

def resolve_and_root_source(root_path)
  out, status = Open3.capture2(
    'nix-build',
    '--out-link',
    root_path,
    helper_file,
    '--arg',
    'repoRoot',
    original_path,
    '--arg',
    'nixpkgsPath',
    @nixpkgs_path,
    '--argstr',
    'nixSystem',
    @nix_system
  )

  unless status.success?
    raise "unable to resolve and GC-root repository source (#{status.exitstatus})"
  end

  output_path = out.lines.map(&:strip).reject(&:empty?).last
  raise 'nix-build returned no repository source output' if output_path.nil?

  File.realpath(File.join(output_path, 'source'))
end

#resolve_path(file_path) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/test-runner/repository_source.rb', line 32

def resolve_path(file_path)
  return nil if file_path.nil? || file_path.empty?

  expanded = File.expand_path(file_path, original_path)
  relative = Pathname.new(expanded).relative_path_from(Pathname.new(original_path))
  return expanded if relative.each_filename.first == '..'

  File.join(path, relative.to_s)
end