Class: CLIntegracon::Subject

Inherits:
Object
  • Object
show all
Defined in:
lib/CLIntegracon/subject.rb

Attributes collapse

Initializer collapse

DSL-like Setter collapse

Interaction collapse

Constructor Details

#initialize(name = 'subject', executable = nil) ⇒ Subject

“Designated” initializer

Parameters:

  • name (String) (defaults to: 'subject')

    The name of the binary

  • executable (String) (defaults to: nil)

    The executable subject statement (optional)



51
52
53
54
55
56
57
58
# File 'lib/CLIntegracon/subject.rb', line 51

def initialize(name='subject', executable=nil)
  self.name = name
  self.executable = executable || name
  self.environment_vars = {}
  self.default_args = []
  self.replace_paths = {}
  self.output_path = 'execution_output.txt'
end

Instance Attribute Details

#default_argsArray<String>

Returns The arguments which will always passed to the executable on launch and should not been passed explicitly every time to the launch method. Those are added behind the arguments given on launch.

Returns:

  • (Array<String>)

    The arguments which will always passed to the executable on launch and should not been passed explicitly every time to the launch method. Those are added behind the arguments given on launch.



25
26
27
# File 'lib/CLIntegracon/subject.rb', line 25

def default_args
  @default_args
end

#environment_varsHash<String,String>

Returns The environment variables which will always been defined for the executable on launch should not been passed explicitly every time to the launch method.

Returns:

  • (Hash<String,String>)

    The environment variables which will always been defined for the executable on launch should not been passed explicitly every time to the launch method.



19
20
21
# File 'lib/CLIntegracon/subject.rb', line 19

def environment_vars
  @environment_vars
end

#executableString

Returns The executable statement to use for the tests.

Returns:

  • (String)

    The executable statement to use for the tests



14
15
16
# File 'lib/CLIntegracon/subject.rb', line 14

def executable
  @executable
end

#nameString

Returns The name of the binary to use for the tests.

Returns:

  • (String)

    The name of the binary to use for the tests



10
11
12
# File 'lib/CLIntegracon/subject.rb', line 10

def name
  @name
end

#output_pathString

Returns The path where the output of the executable will be written to.

Returns:

  • (String)

    The path where the output of the executable will be written to.



36
37
38
# File 'lib/CLIntegracon/subject.rb', line 36

def output_path
  @output_path
end

#replace_pathsHash<String,String>

Returns The replace paths, whose keys are expected to occur in the output, which are not printed relative to the project, when the subject will be executed. These are e.g. paths were side-effects occur, like manipulation of user configurations in dot files or caching-specific directories.

Returns:

  • (Hash<String,String>)

    The replace paths, whose keys are expected to occur in the output, which are not printed relative to the project, when the subject will be executed. These are e.g. paths were side-effects occur, like manipulation of user configurations in dot files or caching-specific directories.



32
33
34
# File 'lib/CLIntegracon/subject.rb', line 32

def replace_paths
  @replace_paths
end

Instance Method Details

#launch(head_arguments = '', tail_arguments = '') ⇒ String

Runs the executable with the given arguments in the temporary directory.

@note: You can check by ‘$?.success?` if the execution succeeded.

Parameters:

  • head_arguments (String) (defaults to: '')

    The arguments to pass to the executable before the default arguments.

  • tail_arguments (String) (defaults to: '')

    The arguments to pass to the executable after the default arguments.

Returns:

  • (String)

    The output, which is emitted while execution from the binary.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/CLIntegracon/subject.rb', line 109

def launch(head_arguments='', tail_arguments='')
  vars = environment_vars.map { |key,value| "#{key}=#{value}" }.join ' '
  args = [head_arguments, default_args, tail_arguments].flatten.compact.select { |s| s.length > 0 }.join ' '
  command = "#{vars} #{executable} #{args} 2>&1"

  output = `#{command}`

  File.open(output_path, 'w') do |file|
    file.write command.sub(executable, name)
    file.write "\n"

    replace_paths.each do |key, path|
      output.gsub!(path, key)
    end

    file.write output
  end

  output
end

#replace_path(path, name = nil) ⇒ Object

Define a path, whose occurrences in the output should been replaced by either its basename or a given placeholder.

Parameters:

  • path (String)

    The path

  • name (String) (defaults to: nil)

    The name of the path, or the basename of the given path



74
75
76
77
# File 'lib/CLIntegracon/subject.rb', line 74

def replace_path(path, name=nil)
  name ||= File.basename path
  self.replace_paths[name] = path
end

#replace_user_path(path, name = nil) ⇒ Object

Define a path in the user directory, whose occurrences in the output should been replaced by either its basename or a given placeholder.

Parameters:

  • path (String)

    The path

  • name (String) (defaults to: nil)

    The name of the path, or the basename of the given path



88
89
90
# File 'lib/CLIntegracon/subject.rb', line 88

def replace_user_path(path, name=nil)
  self.replace_path %r[/Users/.*/#{path.to_s}], name
end