From bbf4b92b1d6059eb4ae96d97e4caa5ce84ef8f40 Mon Sep 17 00:00:00 2001 From: Johan Holmberg Date: Thu, 14 Apr 2016 19:56:38 +0200 Subject: [PATCH] introduce 'import_directory' --- doc/cmdtest.txt | 9 +++- lib/cmdtest/testcase.rb | 19 +++++++++ t/CMDTEST_import_directory.rb | 80 +++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 t/CMDTEST_import_directory.rb diff --git a/doc/cmdtest.txt b/doc/cmdtest.txt index df0f3d3..e40114d 100644 --- a/doc/cmdtest.txt +++ b/doc/cmdtest.txt @@ -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 diff --git a/lib/cmdtest/testcase.rb b/lib/cmdtest/testcase.rb index dd4530d..de4cb59 100644 --- a/lib/cmdtest/testcase.rb +++ b/lib/cmdtest/testcase.rb @@ -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 diff --git a/t/CMDTEST_import_directory.rb b/t/CMDTEST_import_directory.rb new file mode 100644 index 0000000..99b4e9b --- /dev/null +++ b/t/CMDTEST_import_directory.rb @@ -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