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

#source_info ⇒ Object (readonly)

Returns the value of attribute source_info.



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

def source_info
  @source_info
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

#capture_git_identity ⇒ Object (protected)



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/test-runner/repository_source.rb', line 46

def capture_git_identity
  out, _err, status = Open3.capture3('git', '-C', original_path, 'rev-parse', '--show-toplevel', '--absolute-git-dir')
  return unless status.success?

  root, git_dir = out.lines.map(&:strip)
  return unless File.realpath(root) == File.realpath(original_path)

  revision, _err, status = Open3.capture3('git', '-C', original_path, 'rev-parse', '--verify', 'HEAD^{commit}')
  return unless status.success?

  [git_dir, revision.strip]
rescue Errno::ENOENT
  nil
end

#default_root_directory ⇒ Object (protected)



130
131
132
133
134
135
# File 'lib/test-runner/repository_source.rb', line 130

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)



99
100
101
# File 'lib/test-runner/repository_source.rb', line 99

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

#open ⇒ Object



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

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

#resolve_and_root_source(root_path) ⇒ Object (protected)



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/test-runner/repository_source.rb', line 103

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



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

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

#snapshot_dirty_state(git_dir, revision) ⇒ Object (protected)



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/test-runner/repository_source.rb', line 74

def snapshot_dirty_state(git_dir, revision)
  Dir.mktmpdir('source-index-', root_directory) do |dir|
    env = { 'GIT_INDEX_FILE' => File.join(dir, 'index'), 'GIT_WORK_TREE' => path, 'GIT_DIR' => git_dir }
    git = ['git', '-c', 'core.filemode=true']
    _out, _err, status = Open3.capture3(env, *git, 'read-tree', revision)
    return nil unless status.success?

    # Refresh only this private index. Git compares captured bytes with the
    # captured commit, even if the live checkout or HEAD has since moved.
    _out, _err, status = Open3.capture3(env, *git, 'update-index', '-q', '--refresh')
    return nil unless [0, 1].include?(status.exitstatus)

    _out, _err, status = Open3.capture3(env, *git, 'diff-files', '--quiet', '--')
    return nil unless [0, 1].include?(status.exitstatus)
    return true unless status.success?

    # No --exclude-standard: ignored files included by the path snapshot
    # change its identity too. Git's own metadata is not source content.
    extra, _err, status = Open3.capture3(env, *git, 'ls-files', '--others', '--directory', '-z', '--', '.', ':!.git')
    return nil unless status.success?

    !extra.empty?
  end
end

#snapshot_identity(git_identity) ⇒ Object (protected)



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/test-runner/repository_source.rb', line 61

def snapshot_identity(git_identity)
  revision = git_identity&.last
  dirty = snapshot_dirty_state(*git_identity) if git_identity
  revision = nil if dirty.nil?

  {
    sourcePath: path,
    revision:,
    shortRev: revision&.slice(0, 7),
    dirty:
  }.freeze
end