File: util.rb

package info (click to toggle)
facter 4.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,276 kB
  • sloc: ruby: 64,130; sh: 48; makefile: 2
file content (71 lines) | stat: -rw-r--r-- 1,896 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true

def mock_os(os_name)
  allow(OsDetector.instance)
    .to receive(:identifier)
    .and_return(os_name)
end

def mock_fact_loader_with_legacy(os_name, loaded_facts_hash)
  allow(Facter::InternalFactLoader)
    .to receive(:load_with_legacy)
    .with(os_name)
    .and_return(loaded_facts_hash)
end

def mock_fact_loader(os_name, loaded_fact_hash)
  allow(Facter::InternalFactLoader)
    .to receive(:load)
    .with(os_name)
    .and_return(loaded_fact_hash)
end

def mock_query_parser(user_query, loaded_fact_hash)
  query_parser_spy = instance_spy(Facter::QueryParser)
  allow(query_parser_spy)
    .to receive(:parse)
    .with(user_query, loaded_fact_hash)
end

private_methods def allow_attr_change(resolved_fact_mock, fact_value)
  allow(resolved_fact_mock)
    .to receive(:value=)
    .with(fact_value)

  allow(resolved_fact_mock)
    .to receive(:user_query=)
end

def mock_resolved_fact(fact_name, fact_value, user_query = nil, type = :core)
  resolved_fact_mock = instance_double(
    Facter::ResolvedFact, name: fact_name, value: fact_value,
                          user_query: user_query, type: type,
                          legacy?: type == :legacy,
                          core?: type == :core,
                          file: nil,
                          resolves?: fact_name == user_query
  )

  allow_attr_change(resolved_fact_mock, fact_value)
  resolved_fact_mock
end

def mock_fact(fact_class_name, resolved_fact_to_return, fact_name = nil)
  fact_mock = instance_spy(fact_class_name)

  allow(fact_class_name)
    .to receive(:new)
    .and_return(fact_mock)

  allow(fact_class_name)
    .to receive(:call_the_resolver)
    .and_return(resolved_fact_to_return)

  stub_const(fact_class_name.to_s, fact_name) if fact_name.present?

  fact_mock
end

def load_fixture(filename)
  File.open(File.join('spec', 'fixtures', filename))
end