Class: TransformedShellCommandTest

Inherits:
Minitest::Test
  • Object
show all
Defined in:
lib/transformed_shell_command.rb

Instance Method Summary collapse

Instance Method Details

#test_basic_transformationObject



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/transformed_shell_command.rb', line 200

def test_basic_transformation
  regex = /(?<name>\w+):(?<value>\d+)/
  format_str = 'Name: %{name}, Value: %{value}'
  cmd = TransformedShellCommand.new(
    "echo 'user:123'",
    regex: regex,
    format: format_str
  )

  assert cmd.success?
  assert_equal "user:123\n", cmd.stdout
  assert_equal 'Name: user, Value: 123', cmd.transformed_output
end

#test_delegates_to_resultObject



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/transformed_shell_command.rb', line 268

def test_delegates_to_result
  regex = /(?<name>\w+)/
  format_str = '%{name}'
  cmd = TransformedShellCommand.new(
    "echo 'test'",
    regex: regex,
    format: format_str
  )

  assert_kind_of ExecutedShellCommand::Result, cmd.result
  assert_equal "test\n", cmd.stdout
  assert_equal 0, cmd.exit_code
  assert cmd.success?
  assert_kind_of Numeric, cmd.duration
  assert_kind_of Time, cmd.started_at
  assert_kind_of Time, cmd.finished_at
  assert_kind_of Integer, cmd.pid
end

#test_empty_outputObject



349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/transformed_shell_command.rb', line 349

def test_empty_output
  regex = /(?<name>\w+)/
  format_str = '%{name}'
  cmd = TransformedShellCommand.new(
    'true',
    regex: regex,
    format: format_str
  )

  assert cmd.success?
  assert_equal '', cmd.stdout
  assert_equal '', cmd.transformed_output
end

#test_format_string_with_literal_textObject



335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/transformed_shell_command.rb', line 335

def test_format_string_with_literal_text
  regex = /(?<num>\d+)/
  format_str = 'The number is %{num}!'
  cmd = TransformedShellCommand.new(
    "echo '42'",
    regex: regex,
    format: format_str
  )

  assert cmd.success?
  assert_equal "42\n", cmd.stdout
  assert_equal 'The number is 42!', cmd.transformed_output
end

#test_multiline_output_captures_spanning_linesObject



423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/transformed_shell_command.rb', line 423

def test_multiline_output_captures_spanning_lines
  regex = /Start: (?<start>.*?)End: (?<end>.*?)$/m
  format_str = 'From %{start} to %{end}'
  cmd = TransformedShellCommand.new(
    "printf 'Start: alpha\nbeta\ngamma\nEnd: delta\n'",
    regex: regex,
    format: format_str
  )

  assert cmd.success?
  assert_equal "From alpha\nbeta\ngamma\n to delta", cmd.transformed_output
end

#test_multiline_output_no_match_returns_originalObject



436
437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/transformed_shell_command.rb', line 436

def test_multiline_output_no_match_returns_original
  regex = /(?<name>\w+):(?<value>\d+)/
  format_str = 'Name: %{name}, Value: %{value}'
  cmd = TransformedShellCommand.new(
    "printf 'line1\nline2\nline3\n'",
    regex: regex,
    format: format_str
  )

  assert cmd.success?
  assert_equal "line1\nline2\nline3\n", cmd.stdout
  assert_equal "line1\nline2\nline3\n", cmd.transformed_output
end

#test_multiline_output_single_transformationObject



377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/transformed_shell_command.rb', line 377

def test_multiline_output_single_transformation
  regex = /(?<first>\w+)\n(?<second>\w+)\n(?<third>\w+)/m
  format_str = '%{first}-%{second}-%{third}'
  cmd = TransformedShellCommand.new(
    "printf 'one\ntwo\nthree\n'",
    regex: regex,
    format: format_str
  )

  assert cmd.success?
  assert_equal "one\ntwo\nthree\n", cmd.stdout
  assert_equal 'one-two-three', cmd.transformed_output
end

#test_multiline_output_symbol_transformObject



408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/transformed_shell_command.rb', line 408

def test_multiline_output_symbol_transform
  regex = /.*/m
  cmd = TransformedShellCommand.new(
    "printf '  line1\n  line2\n  line3  \n'",
    regex: regex,
    format: :strip
  )

  assert cmd.success?
  assert_equal "  line1\n  line2\n  line3  \n", cmd.stdout
  # strip removes leading/trailing whitespace from entire string
  # Leading spaces from first line and trailing spaces/newline from last line are removed
  assert_equal "line1\n  line2\n  line3", cmd.transformed_output
end

#test_multiline_output_with_multiline_regexObject



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/transformed_shell_command.rb', line 391

def test_multiline_output_with_multiline_regex
  regex = /Name: (?<name>[\w\s]+)\nAge: (?<age>\d+)\nCity: (?<city>[\w\s]+)/m
  format_str = '%{name} (%{age}) from %{city}'
  cmd = TransformedShellCommand.new(
    "printf 'Name: John Doe\nAge: 30\nCity: New York\n'",
    regex: regex,
    format: format_str
  )

  assert cmd.success?
  assert_includes cmd.stdout, 'John Doe'
  assert_includes cmd.stdout, '30'
  assert_includes cmd.stdout, 'New York'
  # Remove trailing newline for comparison
  assert_equal 'John Doe (30) from New York', cmd.transformed_output.chomp
end

#test_multiple_named_groupsObject



254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/transformed_shell_command.rb', line 254

def test_multiple_named_groups
  regex = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/
  format_str = 'Date: %{month}/%{day}/%{year}'
  cmd = TransformedShellCommand.new(
    "echo '2024-12-25'",
    regex: regex,
    format: format_str
  )

  assert cmd.success?
  assert_equal "2024-12-25\n", cmd.stdout
  assert_equal 'Date: 12/25/2024', cmd.transformed_output
end

#test_multiple_placeholders_same_groupObject



363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/transformed_shell_command.rb', line 363

def test_multiple_placeholders_same_group
  regex = /(?<word>\w+)/
  format_str = '%{word} %{word} %{word}'
  cmd = TransformedShellCommand.new(
    "echo 'hello'",
    regex: regex,
    format: format_str
  )

  assert cmd.success?
  assert_equal "hello\n", cmd.stdout
  assert_equal 'hello hello hello', cmd.transformed_output
end

#test_no_match_returns_originalObject



240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/transformed_shell_command.rb', line 240

def test_no_match_returns_original
  regex = /(?<name>\w+):(?<value>\d+)/
  format_str = 'Name: %{name}, Value: %{value}'
  cmd = TransformedShellCommand.new(
    "echo 'no match here'",
    regex: regex,
    format: format_str
  )

  assert cmd.success?
  assert_equal "no match here\n", cmd.stdout
  assert_equal "no match here\n", cmd.transformed_output
end

#test_regex_as_stringObject



322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/transformed_shell_command.rb', line 322

def test_regex_as_string
  regex_str = '(?<name>\\w+):(?<value>\\d+)'
  format_str = 'Name: %{name}, Value: %{value}'
  cmd = TransformedShellCommand.new(
    "echo 'user:123'",
    regex: regex_str,
    format: format_str
  )

  assert cmd.success?
  assert_equal 'Name: user, Value: 123', cmd.transformed_output
end

#test_symbol_transformObject



214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/transformed_shell_command.rb', line 214

def test_symbol_transform
  regex = /.*/
  cmd = TransformedShellCommand.new(
    "echo '  HELLO WORLD  '",
    regex: regex,
    format: :strip
  )

  assert cmd.success?
  assert_equal "  HELLO WORLD  \n", cmd.stdout
  assert_equal 'HELLO WORLD', cmd.transformed_output
end

#test_symbol_transform_upcaseObject



227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/transformed_shell_command.rb', line 227

def test_symbol_transform_upcase
  regex = /.*/
  cmd = TransformedShellCommand.new(
    "echo 'hello'",
    regex: regex,
    format: :upcase
  )

  assert cmd.success?
  assert_equal "hello\n", cmd.stdout
  assert_equal "HELLO\n", cmd.transformed_output
end

#test_with_chdirObject



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/transformed_shell_command.rb', line 287

def test_with_chdir
  regex = /(?<content>.*)/
  format_str = 'Content: %{content}'
  Dir.mktmpdir do |tmpdir|
    test_file = File.join(tmpdir, 'test.txt')
    File.write(test_file, 'hello world')

    cmd = TransformedShellCommand.new(
      'cat test.txt',
      regex: regex,
      format: format_str,
      chdir: tmpdir
    )

    assert cmd.success?
    assert_equal 'hello world', cmd.stdout
    assert_equal 'Content: hello world', cmd.transformed_output
  end
end

#test_with_envObject



307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/transformed_shell_command.rb', line 307

def test_with_env
  regex = /(?<var>\w+)=(?<val>\w+)/
  format_str = '%{var} is %{val}'
  cmd = TransformedShellCommand.new(
    "echo 'TEST_VAR=test_value'",
    regex: regex,
    format: format_str,
    env: { 'CUSTOM_VAR' => 'custom_value' }
  )

  assert cmd.success?
  assert_equal "TEST_VAR=test_value\n", cmd.stdout
  assert_equal 'TEST_VAR is test_value', cmd.transformed_output
end