Class: CachedNestedFileReaderTest

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

Instance Method Summary collapse

Instance Method Details

#setupObject



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/cached_nested_file_reader.rb', line 344

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



368
369
370
371
372
373
374
# File 'lib/cached_nested_file_reader.rb', line 368

def teardown
  @file1.close
  @file1.unlink

  @file2.close
  @file2.unlink
end

#test_caching_functionalityObject



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/cached_nested_file_reader.rb', line 426

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



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# File 'lib/cached_nested_file_reader.rb', line 483

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



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/cached_nested_file_reader.rb', line 442

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



381
382
383
384
385
# File 'lib/cached_nested_file_reader.rb', line 381

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



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

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_shebang_hiddenObject

REQ:SHEBANG_HIDING

Test shebang line detection and filtering

IMPL:SHEBANG_DETECTION
IMPL:SHEBANG_FILTERING
ARCH:SHEBANG_EXTRACTION
REQ:SHEBANG_HIDING


526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
# File 'lib/cached_nested_file_reader.rb', line 526

def test_readlines_with_shebang_hidden
  file_with_shebang = Tempfile.new('test_shebang.txt')
  file_with_shebang.write("#!/usr/bin/env mde\nLine1\nLine2")
  file_with_shebang.rewind

  reader_with_hide = 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=',
    hide_shebang: true
  )

  result = reader_with_hide.readlines(file_with_shebang.path).map(&:to_s)
  assert_equal %w[Line1 Line2], result, 'Shebang line should be filtered when hide_shebang is true'

  file_with_shebang.close
  file_with_shebang.unlink
end

#test_readlines_with_shebang_in_imported_fileObject



582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
# File 'lib/cached_nested_file_reader.rb', line 582

def test_readlines_with_shebang_in_imported_file
  imported_file = Tempfile.new('imported_shebang.txt')
  imported_file.write("#!/usr/bin/env mde\nImportedLine1\nImportedLine2")
  imported_file.rewind

  main_file = Tempfile.new('main_file.txt')
  main_file.write("Line1\n @import #{imported_file.path}\nLine2")
  main_file.rewind

  reader_with_hide = 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=',
    hide_shebang: true
  )

  result = reader_with_hide.readlines(main_file.path).map(&:to_s)
  assert_equal ['Line1', ' ImportedLine1', ' ImportedLine2', 'Line2'], result,
               'Shebang in imported file should be filtered when hide_shebang is true'

  imported_file.close
  imported_file.unlink
  main_file.close
  main_file.unlink
end

#test_readlines_with_shebang_shownObject



554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# File 'lib/cached_nested_file_reader.rb', line 554

def test_readlines_with_shebang_shown
  file_with_shebang = Tempfile.new('test_shebang.txt')
  file_with_shebang.write("#!/usr/bin/env mde\nLine1\nLine2")
  file_with_shebang.rewind

  reader_without_hide = 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=',
    hide_shebang: false
  )

  result = reader_without_hide.readlines(file_with_shebang.path).map(&:to_s)
  assert_equal ['#!/usr/bin/env mde', 'Line1', 'Line2'], result, 'Shebang line should be included when hide_shebang is false'

  file_with_shebang.close
  file_with_shebang.unlink
end

#test_readlines_with_template_delimitersObject



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

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



376
377
378
379
# File 'lib/cached_nested_file_reader.rb', line 376

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

#test_readlines_without_shebangObject



617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
# File 'lib/cached_nested_file_reader.rb', line 617

def test_readlines_without_shebang
  file_without_shebang = Tempfile.new('test_no_shebang.txt')
  file_without_shebang.write("Line1\nLine2\nLine3")
  file_without_shebang.rewind

  reader_with_hide = 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=',
    hide_shebang: true
  )

  result = reader_with_hide.readlines(file_without_shebang.path).map(&:to_s)
  assert_equal %w[Line1 Line2 Line3], result, 'File without shebang should work normally'

  file_without_shebang.close
  file_without_shebang.unlink
end