introduce 'import_directory'

This commit is contained in:
Johan Holmberg 2016-04-14 19:56:38 +02:00
parent 393a92ae33
commit bbf4b92b1d
3 changed files with 107 additions and 1 deletions

@ -190,7 +190,7 @@ the creation of files::
...
The list of such helper functions includes:
``create_file``, ``touch_file``, ``import_file`` and ``ignore_file``.
``create_file``, ``touch_file``, ``import_file`` , ``import_directory`` and ``ignore_file``.
Beside these methods the test can of course also contain arbitrary Ruby-code.
@ -537,6 +537,13 @@ or in the ``setup`` method.
the current directory inside the "work directory" at the time
of the call.
``import_directory(src, tgt)``
Copy a directory tree from outside of the "work directory" to inside.
The ``src`` path is evaluated relative to the current directory
when ``cmdtest`` was called. The ``tgt`` is evaluated relative to
the current directory inside the "work directory" at the time
of the call. It is an error if ``tgt`` already exists.
``prepend_local_path(dir)``
Prepend the given directory to the ``PATH`` so commands executed via ``cmd``
are looked up using the modified ``PATH``. The argument ``dir`` is evaluated

@ -96,6 +96,25 @@ module Cmdtest
end
end
#------------------------------
# Import directory into the "workdir" from the outside world.
# The source is found relative to the current directory when "cmdtest"
# was invoked. The target is created inside the "workdir" relative to
# the current directory at the time of the call.
def import_directory(src, tgt)
src_path = File.expand_path(src, @_runner.test_files_top)
tgt_path = _cwd_path(tgt)
FileUtils.mkdir_p(File.dirname(tgt_path))
if File.exists?(tgt_path)
raise UsageError, "'import_directory' target argument already exist: '#{tgt}'"
elsif File.directory?(src_path)
FileUtils.cp_r(src_path, tgt_path)
else
raise UsageError, "'import_directory' argument not a directory: '#{src}'"
end
end
#------------------------------
# Create a file inside the "workdir".
# The content can be specified either as an Array of lines or as

@ -0,0 +1,80 @@
require "selftest_utils"
class CMDTEST_import_directory < Cmdtest::Testcase
include SelftestUtils
#----------------------------------------
# import_directory
#----------------------------------------
def test_import_directory_ERROR
create_file "file1.dir/file1.txt", "This is file1.dir/file1.txt\n"
create_file "file2.dir/file2.txt", "This is file2.dir/file2.txt\n"
create_CMDTEST_foo [
"import_directory 'file1.dir', 'qwerty1.dir'",
"import_directory 'file2.dir', 'qwerty1.dir'",
]
cmd_cmdtest do
stdout_equal /CAUGHT EXCEPTION:/
stdout_equal /'import_directory' target argument already exist: 'qwerty1.dir'/
exit_nonzero
end
end
#----------------------------------------
def test_import_directory_DIFFERENT_DIRS
create_file "file1.dir/file1.txt", "This is file1.dir/file1.txt\n"
create_file "file2.dir/file2.txt", "This is file2.dir/file2.txt\n"
create_CMDTEST_foo [
"import_directory 'file1.dir', 'qwerty1.dir'",
"import_directory 'file2.dir', 'subdir/qwerty2.dir'",
"",
"cmd 'cat.rb qwerty1.dir/file1.txt subdir/qwerty2.dir/file2.txt' do",
" stdout_equal [",
" 'This is file1.dir/file1.txt',",
" 'This is file2.dir/file2.txt',",
" ]",
"end",
]
cmd_cmdtest do
stdout_equal [
"### cat.rb qwerty1.dir/file1.txt subdir/qwerty2.dir/file2.txt",
]
end
end
#----------------------------------------
def test_import_directory_AFTER_CHDIR
create_file "file1.dir/file1.txt", "This is file1.dir/file1.txt\n"
create_file "file2.dir/file2.txt", "This is file2.dir/file2.txt\n"
create_CMDTEST_foo [
"dir_mkdir('dir')",
"chdir('dir')",
"import_directory 'file1.dir', 'qwerty1.dir'",
"import_directory 'file2.dir', 'subdir/qwerty2.dir'",
"",
"cmd 'cat.rb qwerty1.dir/file1.txt subdir/qwerty2.dir/file2.txt' do",
" stdout_equal [",
" 'This is file1.dir/file1.txt',",
" 'This is file2.dir/file2.txt',",
" ]",
"end",
]
cmd_cmdtest do
stdout_equal [
"### cat.rb qwerty1.dir/file1.txt subdir/qwerty2.dir/file2.txt",
]
end
end
end