Class: TestRunner::NixCli

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

Constant Summary collapse

DEFAULT_SYSTEM =
'x86_64-linux'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(system: DEFAULT_SYSTEM, test_config_path: nil, repo_root: nil) ⇒ NixCli

Returns a new instance of NixCli.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/test-runner/nix_cli.rb', line 10

def initialize(system: DEFAULT_SYSTEM, test_config_path: nil, repo_root: nil)
  @system = if system.nil? || system.empty?
              DEFAULT_SYSTEM
            else
              system
            end
  @repo_root = File.expand_path(repo_root || ENV['TEST_RUNNER_REPO_ROOT'] || Dir.pwd)
  @test_config_path =
    if test_config_path.nil? || test_config_path.empty?
      nil
    else
      File.expand_path(test_config_path, @repo_root)
    end
end

Instance Attribute Details

#repo_root ⇒ Object (readonly)

Returns the value of attribute repo_root.



8
9
10
# File 'lib/test-runner/nix_cli.rb', line 8

def repo_root
  @repo_root
end

#system ⇒ Object (readonly)

Returns the value of attribute system.



8
9
10
# File 'lib/test-runner/nix_cli.rb', line 8

def system
  @system
end

#test_config_path ⇒ Object (readonly)

Returns the value of attribute test_config_path.



8
9
10
# File 'lib/test-runner/nix_cli.rb', line 8

def test_config_path
  @test_config_path
end

Instance Method Details

#base_cmd(*cmd, mode:, test_path: nil, test_args: nil) ⇒ Object (protected)



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/test-runner/nix_cli.rb', line 82

def base_cmd(*cmd, mode:, test_path: nil, test_args: nil)
  ret = [
    *cmd,
    helper_file,
    '--arg',
    'repoRoot',
    repo_root,
    '--argstr',
    'system',
    system,
    '--argstr',
    'mode',
    mode
  ]

  ret += ['--arg', 'testConfigPath', test_config_path] if test_config_path
  ret += ['--argstr', 'testPath', test_path] if test_path
  ret += ['--argstr', 'testArgsJson', JSON.generate(test_args)] unless test_args.nil?
  ret
end

#build_cmd(mode:, test_path:, test_args:, out_link:) ⇒ Object (protected)



71
72
73
74
75
76
77
78
79
80
# File 'lib/test-runner/nix_cli.rb', line 71

def build_cmd(mode:, test_path:, test_args:, out_link:)
  base_cmd(
    'nix-build',
    '--out-link',
    out_link,
    mode:,
    test_path:,
    test_args:
  )
end

#build_test_batch(selection, out_link, max_jobs:, log_path:) ⇒ Object



37
38
39
40
41
# File 'lib/test-runner/nix_cli.rb', line 37

def build_test_batch(selection, out_link, max_jobs:, log_path:)
  cmd = build_cmd(mode: 'testBatch', test_path: nil, test_args: nil, out_link:)
  cmd += ['--argstr', 'selectedTestsJson', JSON.generate(selection), '--max-jobs', max_jobs.to_s]
  run!(*cmd, log_path:)
end

#build_test_json(path, out_link, test_args: {}) ⇒ Object



33
34
35
# File 'lib/test-runner/nix_cli.rb', line 33

def build_test_json(path, out_link, test_args: {})
  run!(*build_cmd(mode: 'testJson', test_path: path, test_args:, out_link: out_link))
end

#capture_output(*cmd) ⇒ Object (protected)



103
104
105
106
107
108
# File 'lib/test-runner/nix_cli.rb', line 103

def capture_output(*cmd)
  out, status = Open3.capture2(*cmd)
  raise "#{cmd.join(' ')} failed (#{status_description(status)})" unless status.success?

  out
end

#eval_cmd(mode:, test_path: nil) ⇒ Object (protected)



60
61
62
63
64
65
66
67
68
69
# File 'lib/test-runner/nix_cli.rb', line 60

def eval_cmd(mode:, test_path: nil)
  base_cmd(
    'nix-instantiate',
    '--eval',
    '--strict',
    '--json',
    mode:,
    test_path:
  )
end

#eval_test_meta(path) ⇒ Object



29
30
31
# File 'lib/test-runner/nix_cli.rb', line 29

def eval_test_meta(path)
  capture_output(*eval_cmd(mode: 'testsMetaOne', test_path: path))
end

#eval_tests_meta_all ⇒ Object



25
26
27
# File 'lib/test-runner/nix_cli.rb', line 25

def eval_tests_meta_all
  capture_output(*eval_cmd(mode: 'testsMetaAll'))
end

#helper_file ⇒ Object (protected)



56
57
58
# File 'lib/test-runner/nix_cli.rb', line 56

def helper_file
  File.expand_path('../../nix/evaluate-tests.nix', __dir__)
end

#local_max_jobs ⇒ Object

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
51
52
# File 'lib/test-runner/nix_cli.rb', line 43

def local_max_jobs
  config = JSON.parse(capture_output('nix', 'config', 'show', '--json'))
  value = config.fetch('max-jobs').fetch('value')
  # Nix normally serializes the resolved value even for max-jobs=auto.
  value = ResourcePool.detect_cpus if value == 'auto'
  jobs = Integer(value)
  raise ArgumentError, 'Nix max-jobs must be non-negative' if jobs < 0

  jobs
end

#run!(*cmd, log_path: nil) ⇒ Object (protected)



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/test-runner/nix_cli.rb', line 110

def run!(*cmd, log_path: nil)
  log = File.open(log_path, 'a') if log_path
  options = { pgroup: true }
  options.merge!(out: log, err: %i[child out]) if log
  pid = spawn(*cmd, **options)
  _pid, status = Process.wait2(pid)
  return if status.success?

  log&.puts("Preparation build failed (#{status_description(status)})")
  raise "#{cmd.join(' ')} failed (#{status_description(status)})"
rescue SignalException => e
  if pid
    begin
      Process.kill(e.signo, -pid)
    rescue Errno::ESRCH
      # The owned client already exited.
    end
    begin
      Process.wait(pid)
    rescue Errno::ECHILD
      # Cancellation can arrive after wait2 has already reaped the client.
    end
  end
  log&.puts("Preparation interrupted by signal #{e.signo} (#{Signal.signame(e.signo)})")
  raise
ensure
  log&.close
end

#status_description(status) ⇒ Object (protected)



139
140
141
142
143
# File 'lib/test-runner/nix_cli.rb', line 139

def status_description(status)
  return status.exitstatus.to_s unless status.signaled?

  "signal #{status.termsig} (#{Signal.signame(status.termsig)})"
end