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.



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

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_rootObject (readonly)

Returns the value of attribute repo_root.



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

def repo_root
  @repo_root
end

#systemObject (readonly)

Returns the value of attribute system.



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

def system
  @system
end

#test_config_pathObject (readonly)

Returns the value of attribute test_config_path.



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

def test_config_path
  @test_config_path
end

Instance Method Details

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/test-runner/nix_cli.rb', line 63

def base_cmd(*cmd, mode:, test_path: 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
end

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



53
54
55
56
57
58
59
60
61
# File 'lib/test-runner/nix_cli.rb', line 53

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

#build_test_json(path, out_link) ⇒ Object



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

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

#capture_output(*cmd) ⇒ Object (protected)



83
84
85
86
87
88
# File 'lib/test-runner/nix_cli.rb', line 83

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

  out
end

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



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

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

#eval_test_meta(path) ⇒ Object



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

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

#eval_tests_meta_allObject



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

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

#helper_fileObject (protected)



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

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

#run!(*cmd) ⇒ Object (protected)



90
91
92
93
94
# File 'lib/test-runner/nix_cli.rb', line 90

def run!(*cmd)
  pid = spawn(*cmd)
  Process.wait(pid)
  raise "#{cmd.join(' ')} failed (#{$?.exitstatus})" unless $?.exitstatus == 0
end