lib/saved_assets.rb in markdown_exec-2.3.0 vs lib/saved_assets.rb in markdown_exec-2.4.0
- old
+ new
@@ -1,10 +1,11 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
# encoding=utf-8
require_relative 'namer'
+require_relative 'object_present'
module MarkdownExec
# SavedAsset
#
# This class provides utilities to format and derive asset names based on
@@ -23,20 +24,19 @@
# @param time [Time] the time object for formatting
# @param blockname [String] the block name to include
# @param ftime [String] the time format (default: DEFAULT_FTIME)
# @param exts [String] the extension to append (default: '.sh')
def initialize(
- saved_asset_format:, filename: nil, prefix: nil, time: nil, blockname: nil,
- ftime: DEFAULT_FTIME, exts: nil,
- mark: nil, join_str: nil
+ saved_asset_format:, blockname: nil, exts: nil,
+ filename: nil, ftime: DEFAULT_FTIME, join_str: nil,
+ mark: nil, prefix: nil, time: nil
)
- @filename = filename ? filename.pub_name : '*' # [String] the name of the file
- @prefix = prefix || '*' # [String] the prefix to use
- @time = time ? time.strftime(ftime) : '*' # [Time] the time object for formatting
- @blockname = blockname ? blockname.pub_name : '*' # [String] the block name to include
- # @ftime = ftime # [String] the time format (default: DEFAULT_FTIME)
- @exts = exts || '.*' # [String] the extension to append (default: '.sh')
+ @filename = filename.present? ? filename.pub_name : '*'
+ @prefix = prefix || '*'
+ @time = time ? time.strftime(ftime) : '*'
+ @blockname = blockname ? blockname.pub_name : '*'
+ @exts = exts || '.*'
@mark = mark || MARK_STR
@join_str = join_str || JOIN_STR
@saved_asset_format = saved_asset_format
end
@@ -62,102 +62,113 @@
return if $PROGRAM_NAME != __FILE__
require 'minitest/autorun'
+SAVED_ASSET_FORMAT = '%{prefix}%{join}%{time}%{join}%{filename}%{join}' \
+ '%{mark}%{join}%{blockname}%{join}%{exts}'
+
class SavedAssetTest < Minitest::Test
def test_script_name_with_special_characters_in_blockname
filename = 'sample.txt'
prefix = 'test'
time = Time.new(2023, 1, 1, 12, 0, 0)
blockname = 'block/1:2'
- expected_name = 'test_2023-01-01-12-00-00_sample_txt_,_block_1_2.sh'
- assert_equal expected_name, MarkdownExec::SavedAsset.script_name(
- filename: filename, prefix: prefix, time: time, blockname: blockname
- )
+ expected_name = 'test_2023-01-01-12-00-00_sample_txt_~_block_1_2_.*'
+ assert_equal expected_name, MarkdownExec::SavedAsset.new(
+ blockname: blockname, filename: filename, prefix: prefix,
+ saved_asset_format: SAVED_ASSET_FORMAT, time: time
+ ).generate_name
end
def test_stdout_name_with_special_characters_in_blockname
filename = 'sample.txt'
prefix = 'test'
time = Time.new(2023, 1, 1, 12, 0, 0)
blockname = 'block/1:2'
- expected_name = 'test_2023-01-01-12-00-00_sample_txt_,_block_1_2.out.txt'
- assert_equal expected_name, MarkdownExec::SavedAsset.stdout_name(
+ expected_name = 'test_2023-01-01-12-00-00_sample_txt_~_block_1_2_.*'
+ assert_equal expected_name, MarkdownExec::SavedAsset.new(
+ saved_asset_format: SAVED_ASSET_FORMAT,
filename: filename, prefix: prefix, time: time, blockname: blockname
- )
+ ).generate_name
end
def test_wildcard_name_with_all_parameters
filename = 'sample.txt'
prefix = 'test'
time = Time.new(2023, 1, 1, 12, 0, 0)
blockname = 'block/1:2'
- expected_wildcard = 'test_2023-01-01-12-00-00_sample_txt_,_block_1_2.sh'
+ expected_wildcard = 'test_2023-01-01-12-00-00_sample_txt_~_block_1_2_.*'
- assert_equal expected_wildcard, MarkdownExec::SavedAsset.wildcard_name(
+ assert_equal expected_wildcard, MarkdownExec::SavedAsset.new(
+ saved_asset_format: SAVED_ASSET_FORMAT,
filename: filename, prefix: prefix, time: time, blockname: blockname
- )
+ ).generate_name
end
def test_wildcard_name_with_missing_time
filename = 'sample.txt'
prefix = 'test'
time = nil
blockname = 'block/1:2'
- expected_wildcard = 'test_*_sample_txt_,_block_1_2.sh'
+ expected_wildcard = 'test_*_sample_txt_~_block_1_2_.*'
- assert_equal expected_wildcard, MarkdownExec::SavedAsset.wildcard_name(
+ assert_equal expected_wildcard, MarkdownExec::SavedAsset.new(
+ saved_asset_format: SAVED_ASSET_FORMAT,
filename: filename, prefix: prefix, time: time, blockname: blockname
- )
+ ).generate_name
end
def test_wildcard_name_with_missing_filename
filename = nil
prefix = 'test'
time = Time.new(2023, 1, 1, 12, 0, 0)
blockname = 'block/1:2'
- expected_wildcard = 'test_2023-01-01-12-00-00_*_,_block_1_2.sh'
+ expected_wildcard = 'test_2023-01-01-12-00-00_*_~_block_1_2_.*'
- assert_equal expected_wildcard, MarkdownExec::SavedAsset.wildcard_name(
+ assert_equal expected_wildcard, MarkdownExec::SavedAsset.new(
+ saved_asset_format: SAVED_ASSET_FORMAT,
filename: filename, prefix: prefix, time: time, blockname: blockname
- )
+ ).generate_name
end
def test_wildcard_name_with_missing_prefix
filename = 'sample.txt'
prefix = nil
time = Time.new(2023, 1, 1, 12, 0, 0)
blockname = 'block/1:2'
- expected_wildcard = '*_2023-01-01-12-00-00_sample_txt_,_block_1_2.sh'
+ expected_wildcard = '*_2023-01-01-12-00-00_sample_txt_~_block_1_2_.*'
- assert_equal expected_wildcard, MarkdownExec::SavedAsset.wildcard_name(
+ assert_equal expected_wildcard, MarkdownExec::SavedAsset.new(
+ saved_asset_format: SAVED_ASSET_FORMAT,
filename: filename, prefix: prefix, time: time, blockname: blockname
- )
+ ).generate_name
end
def test_wildcard_name_with_missing_blockname
filename = 'sample.txt'
prefix = 'test'
time = Time.new(2023, 1, 1, 12, 0, 0)
blockname = nil
- expected_wildcard = 'test_2023-01-01-12-00-00_sample_txt_,_*.sh'
+ expected_wildcard = 'test_2023-01-01-12-00-00_sample_txt_~_*_.*'
- assert_equal expected_wildcard, MarkdownExec::SavedAsset.wildcard_name(
+ assert_equal expected_wildcard, MarkdownExec::SavedAsset.new(
+ saved_asset_format: SAVED_ASSET_FORMAT,
filename: filename, prefix: prefix, time: time, blockname: blockname
- )
+ ).generate_name
end
def test_wildcard_name_with_all_missing
filename = nil
prefix = nil
time = nil
blockname = nil
- expected_wildcard = '*_*_*_,_*.sh'
+ expected_wildcard = '*_*_*_~_*_.*'
- assert_equal expected_wildcard, MarkdownExec::SavedAsset.wildcard_name(
+ assert_equal expected_wildcard, MarkdownExec::SavedAsset.new(
+ saved_asset_format: SAVED_ASSET_FORMAT,
filename: filename, prefix: prefix, time: time, blockname: blockname
- )
+ ).generate_name
end
end