Module: PDQTest::Util

Defined in:
lib/pdqtest/util.rb

Constant Summary collapse

PDQENV =
'export TERM=xterm LC_ALL=C PATH=/usr/local/bats/bin:/opt/puppetlabs/puppet/bin:$PATH;'

Class Method Summary collapse

Class Method Details

.app_dirObject



9
10
11
# File 'lib/pdqtest/util.rb', line 9

def self.app_dir
  ".pdqtest"
end

.app_dir_expandedObject



13
14
15
# File 'lib/pdqtest/util.rb', line 13

def self.app_dir_expanded
  joinp(Dir.home, app_dir)
end

.clean_envObject

environment - rvm and bundler 🤮



97
98
99
100
101
102
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
129
130
131
132
# File 'lib/pdqtest/util.rb', line 97

def self.clean_env
  env = ENV.reject { |e|
    [
      "BUNDLER_ORIG_BUNDLER_ORIG_MANPATH",
      "BUNDLER_ORIG_BUNDLER_VERSION",
      "BUNDLER_ORIG_BUNDLE_BIN_PATH",
      "BUNDLER_ORIG_BUNDLE_GEMFILE",
      "BUNDLER_ORIG_GEM_HOME",
      "BUNDLER_ORIG_GEM_PATH",
      "BUNDLER_ORIG_MANPATH",
      "BUNDLER_ORIG_PATH",
      "BUNDLER_ORIG_RB_USER_INSTALL",
      "BUNDLER_ORIG_RUBYLIB",
      "BUNDLER_ORIG_RUBYOPT",
      "BUNDLER_VERSION",
      "BUNDLE_BIN_PATH",
      "BUNDLE_GEMFILE",
      "GEM_HOME",
      "GEM_PATH",
      "MANPATH",
      "PROMPT",
      "RUBYLIB",
      "RUBYOPT",

      # more random crap
      "rvm_bin_path",
      "IRBRC",
      "MY_RUBY_HOME",
      "rvm_path",
      "rvm_prefix",
      "RUBY_VERSION"
    ].include? e
  }

  env
end

.host_platformObject



17
18
19
# File 'lib/pdqtest/util.rb', line 17

def self.host_platform
  Gem.win_platform? ? :windows : :linux
end

.is_windowsObject



21
22
23
# File 'lib/pdqtest/util.rb', line 21

def self.is_windows
  host_platform == :windows
end

.joinp(*args) ⇒ Object

File.join joins paths with ‘/` always so we must create our own function to join paths correctly for windows since using `/` in docker is not gonna work



49
50
51
52
# File 'lib/pdqtest/util.rb', line 49

def self.joinp(*args)
  File.join(args).gsub(File::SEPARATOR,
                               File::ALT_SEPARATOR || File::SEPARATOR)
end

Parameters:

  • link

    the symlink file

  • target

    the real file



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pdqtest/util.rb', line 62

def self.mk_link(link, target)
  $logger.debug "symlink: #{link} <==> #{target}"
  if Util.is_windows
    Emoji.emoji_message(
        :shame,
        "symlinks not supported by ruby/puppet on windows doing COPY instead")
    # broken until ruby/puppet fixed cmd = "#{rm(link)} ; cmd /C mklink /D '#{link}' '#{target}'"

    # for regular files use `copy-time`, for trees use `robocopy` - since
    # files are in the container we have no idea what is a file or not so
    # just guess based on presence of filename extension
    if link =~ /\.[\w]+$/
      cmd = "copy-item '#{target}' -destination '#{link}'"
    else
      cmd = "robocopy '#{target}' '#{link}' /NFL /NDL /NJH /NJS /nc /ns /np /MIR /XD .git fixtures"
    end
  else
    cmd = "#{rm(link)} && mkdir -p #{File.dirname(link)} && ln -s #{target} #{link}"
  end

  cmd
end

.resource_path(resource) ⇒ Object



5
6
7
# File 'lib/pdqtest/util.rb', line 5

def self.resource_path(resource)
  joinp(File.dirname(File.expand_path(__FILE__)), "../../res/#{resource}")
end

.rm(f) ⇒ Object

3x“ –> 1x” seems only way to escape quotes to keep cmd.exe happy. also need to use double quotes for all args or they get eaten



56
57
58
# File 'lib/pdqtest/util.rb', line 56

def self.rm(f)
  is_windows ? "if (test-path '#{f}'){ Remove-Item '#{f}' -Recurse -Force}" : "rm -rf #{f}"
end

.shellObject



25
26
27
28
# File 'lib/pdqtest/util.rb', line 25

def self.shell
  # cmd.exe is basically broken under docker, use powershell
  is_windows ? "powershell" : "bash"
end

.volumes2binds(volumes) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/pdqtest/util.rb', line 85

def self.volumes2binds(volumes)
  # {test_dir => {pwd => 'rw'} + VOLUMES
  # ...to...
  # "pwd:test_dir:rw",
  volumes.map { |container_dir, host_mapping|
    host_mapping.map { |dir, mode|
      "#{dir}:#{container_dir}:#{mode}"
    }.first
  }
end

.wrap_cmd(cmd) ⇒ Object

need to wrap commands with shell to gain access to shell functions like ‘cd` etc



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pdqtest/util.rb', line 32

def self.wrap_cmd(cmd)
  if cmd.strip.empty? || cmd == "bash" || cmd == "powershell"
    raise "Missing command to wrap!"
  end

  if is_windows
    wrapped = [shell, "-command", "#{cmd} ; exit $LastExitCode"]
  else
    wrapped = [shell, "-c", "#{PDQENV} #{cmd}"]
  end

  wrapped
end