Class: CachedNestedFileReaderTest

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

Instance Method Summary collapse

Instance Method Details

#setupObject



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/cached_nested_file_reader.rb', line 325

def setup
  @file2 = Tempfile.new('test2.txt')
  @file2.write("ImportedLine1\nImportedLine2")
  @file2.rewind

  @file1 = Tempfile.new('test1.txt')
  @file1.write("Line1\nLine2\n @import #{@file2.path}\nLine3")
  @file1.rewind
  @reader = CachedNestedFileReader.new(
    import_directive_line_pattern:
      /^(?<indention> *)@import +(?<name>\S+)(?<params>(?: +[A-Za-z_]\w*=(?:"[^"]*"|'[^']*'|\S+))*) *$/,
    import_directive_parameter_scan:
      /([A-Za-z_]\w*)(:=|\?=|!=|=)(?:"([^"]*)"|'([^']*)'|(\S+))/,
    import_parameter_variable_assignment: '%{key}=%{value}',
    shell: 'bash',
    shell_block_name: '(document_shell)',
    symbol_command_substitution: ':c=',
    symbol_evaluated_expression: ':e=',
    symbol_raw_literal: '=',
    symbol_force_quoted_literal: ':q=',
    symbol_variable_reference: ':v='
  )
end

#teardownObject



349
350
351
352
353
354
355
# File 'lib/cached_nested_file_reader.rb', line 349

def teardown
  @file1.close
  @file1.unlink

  @file2.close
  @file2.unlink
end

#test_caching_functionalityObject



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

def test_caching_functionality
  # First read

  result1 = @reader.readlines(@file2.path).map(&:to_s)

  # Simulate file content change
  @file2.reopen(@file2.path, 'w') { |f| f.write('ChangedLine') }

  # Second read (should read from cache, not the changed file)
  result2 = @reader.readlines(@file2.path, clear_cache: false,
                                           read_cache: true).map(&:to_s)

  assert_equal result1, result2
  assert_equal %w[ImportedLine1 ImportedLine2], result2
end

#test_import_caching_with_different_parametersObject



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/cached_nested_file_reader.rb', line 464

def test_import_caching_with_different_parameters
  # Create a file that will be imported with different parameters
  template_file = Tempfile.new('template.txt')
  template_file.write('Hello NAME, your ID is ID')
  template_file.rewind

  # Create a file that imports the same file with different parameters
  importing_file = Tempfile.new('importing_different.txt')
  importing_file.write("Users:\n @import #{template_file.path} NAME=Alice ID=123\n @import #{template_file.path} NAME=Bob ID=456\nEnd")
  importing_file.rewind

  # Track how many times the template file is actually read
  read_count = 0
  original_readlines = File.method(:readlines)
  File.define_singleton_method(:readlines) do |filename, **opts|
    if filename == template_file.path
      read_count += 1
    end
    original_readlines.call(filename, **opts)
  end

  result = @reader.readlines(importing_file.path).map(&:to_s)

  # The template file should be read twice since parameters are different
  assert_equal 2, read_count,
               'Template file should be read twice when imported with different parameters'

  # Verify the content is correct
  expected = ['Users:', ' Hello Alice, your 123 is 123',
              ' Hello Bob, your 456 is 456', 'End']
  assert_equal expected, result

  # Restore original method
  File.define_singleton_method(:readlines, original_readlines)

  template_file.close
  template_file.unlink
  importing_file.close
  importing_file.unlink
end

#test_import_caching_with_same_parametersObject



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/cached_nested_file_reader.rb', line 423

def test_import_caching_with_same_parameters
  # Create a file that will be imported multiple times
  shared_file = Tempfile.new('shared.txt')
  shared_file.write("Shared content line 1\nShared content line 2")
  shared_file.rewind

  # Create a file that imports the same file multiple times with same parameters
  importing_file = Tempfile.new('importing_multiple.txt')
  importing_file.write("Start\n @import #{shared_file.path} PARAM=value\nMiddle\n @import #{shared_file.path} PARAM=value\nEnd")
  importing_file.rewind

  # Track how many times the shared file is actually read
  read_count = 0
  original_readlines = File.method(:readlines)
  File.define_singleton_method(:readlines) do |filename, **opts|
    if filename == shared_file.path
      read_count += 1
    end
    original_readlines.call(filename, **opts)
  end

  result = @reader.readlines(importing_file.path).map(&:to_s)

  # The shared file should only be read once, not twice
  assert_equal 1, read_count,
               'Shared file should only be read once when imported with same parameters'

  # Verify the content is correct
  expected = ['Start', ' Shared content line 1', ' Shared content line 2',
              'Middle', ' Shared content line 1', ' Shared content line 2', 'End']
  assert_equal expected, result

  # Restore original method
  File.define_singleton_method(:readlines, original_readlines)

  shared_file.close
  shared_file.unlink
  importing_file.close
  importing_file.unlink
end

#test_readlines_with_importsObject



362
363
364
365
366
# File 'lib/cached_nested_file_reader.rb', line 362

def test_readlines_with_imports
  result = @reader.readlines(@file1.path).map(&:to_s)
  assert_equal ['Line1', 'Line2', ' ImportedLine1', ' ImportedLine2', 'Line3'],
               result
end

#test_readlines_with_imports_and_substitutionsObject



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/cached_nested_file_reader.rb', line 368

def test_readlines_with_imports_and_substitutions
  file_with_substitution = Tempfile.new('test_substitution.txt')
  file_with_substitution.write("Server: HOST\nPort: PORT")
  file_with_substitution.rewind

  file_importing = Tempfile.new('test_importing.txt')
  file_importing.write("Config:\n @import #{file_with_substitution.path} HOST=localhost PORT=8080\nEnd")
  file_importing.rewind

  result = @reader.readlines(file_importing.path).map(&:to_s)
  assert_equal ['Config:', ' Server: localhost', ' Port: 8080', 'End'],
               result

  file_with_substitution.close
  file_with_substitution.unlink
  file_importing.close
  file_importing.unlink
end

#test_readlines_with_template_delimitersObject



387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/cached_nested_file_reader.rb', line 387

def test_readlines_with_template_delimiters
  file_with_template = Tempfile.new('test_template.txt')
  file_with_template.write("API_URL=${API_URL}\nVERSION={{VERSION}}")
  file_with_template.rewind

  file_importing = Tempfile.new('test_importing.txt')
  file_importing.write("Config:\n @import #{file_with_template.path} API_URL=https://api.example.com VERSION=1.2.3\nEnd")
  file_importing.rewind

  result = @reader.readlines(file_importing.path,
                             use_template_delimiters: true).map(&:to_s)
  assert_equal ['Config:', ' API_URL=https://api.example.com', ' VERSION=1.2.3', 'End'],
               result

  file_with_template.close
  file_with_template.unlink
  file_importing.close
  file_importing.unlink
end

#test_readlines_without_importsObject



357
358
359
360
# File 'lib/cached_nested_file_reader.rb', line 357

def test_readlines_without_imports
  result = @reader.readlines(@file2.path).map(&:to_s)
  assert_equal %w[ImportedLine1 ImportedLine2], result
end