diff --git a/t/CMDTEST_assert.rb b/t/CMDTEST_assert.rb new file mode 100644 index 0000000..fec0f43 --- /dev/null +++ b/t/CMDTEST_assert.rb @@ -0,0 +1,85 @@ + +require "selftest_utils" + +class CMDTEST_assert < Cmdtest::Testcase + + include SelftestUtils + + #---------------------------------------- + # a positive assert is "quiet" + + def test_assert_CORRECT + create_CMDTEST_foo [ + " cmd 'true.rb' do", + " assert true", + " end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + ] + end + end + + #---------------------------------------- + # a negative assert is prints an error + + def test_assert_INCORRECT + create_CMDTEST_foo [ + " cmd 'true.rb' do", + " assert false", + " end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + "--- ERROR: assertion failed", + ] + exit_nonzero + end + end + + #---------------------------------------- + # an assert can have an extra message parameter, + # that is printed if the assert is negative + + def test_assert_INCORRECT_WITH_MSG + create_CMDTEST_foo [ + " cmd 'true.rb' do", + " assert false, 'got false'", + " end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + "--- ERROR: assertion: got false", + ] + exit_nonzero + end + end + + #---------------------------------------- + # two negative asserts are both reported + + def test_assert_INCORRECT_WITH_MSG_2 + create_CMDTEST_foo [ + " cmd 'true.rb' do", + " assert false, 'got false 1'", + " assert false, 'got false 2'", + " end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + "--- ERROR: assertion: got false 1", + "--- ERROR: assertion: got false 2", + ] + exit_nonzero + end + end + +end diff --git a/t/CMDTEST_cmd.rb b/t/CMDTEST_cmd.rb new file mode 100644 index 0000000..f2825d1 --- /dev/null +++ b/t/CMDTEST_cmd.rb @@ -0,0 +1,184 @@ + +require "selftest_utils" + +class CMDTEST_cmd < Cmdtest::Testcase + + include SelftestUtils + + #---------------------------------------- + # use of "cmd" + #---------------------------------------- + + + def test_cmd_array_argument + create_CMDTEST_foo [ + "cmd ['lines.rb', 'this is an argument', 'and another'] do", + " stdout_equal [", + " 'this is an argument',", + " 'and another',", + " ]", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + '### lines.rb "this is an argument" "and another"', + ] + end + end + + #----------------------------------- + + def test_cmd_only_some_arguments_need_quoting + create_CMDTEST_foo [ + "cmd ['lines.rb', 'arg1', 'a r g 2', ''] do", + " stdout_equal [", + " 'arg1',", + " 'a r g 2',", + " '',", + " ]", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + '### lines.rb arg1 "a r g 2" ""', + ] + end + end + + #----------------------------------- + + def test_cmd_array_with_no_arguments + create_CMDTEST_foo [ + "cmd ['true.rb'] do", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + ] + end + end + + #----------------------------------- + + def test_array_with_no_arguments_II + create_CMDTEST_foo [ + "cmd ['false.rb'] do", + " exit_nonzero", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### false.rb", + ] + end + end + + #----------------------------------- + + def test_array_with_QQ_and_BACKSLASH_in_arguments + create_CMDTEST_foo <<'_END_' + cmd ["clines", "emb\"edded 1", "emb\\edded 2", "emb\\edd\"ed 3"] do + stdout_equal [ + "emb\"edded 1", + "emb\\edded 2", + "emb\\edd\"ed 3", + ] + end +_END_ + + cmd_cmdtest do + stdout_equal [ + /### .*clines.*/, + ] + end + end + + #----------------------------------- + + def test_array_with_DOLLAR_arguments_1 + create_CMDTEST_foo <<'_END_' + cmd ["clines", "emb$edded 1", "emb$$edded 2"] do + stdout_equal [ + "emb$edded 1", + "emb$$edded 2", + ] + end +_END_ + + cmd_cmdtest do + stdout_equal [ + /### .*clines.*/, + ] + end + end + + #----------------------------------- + + def test_array_with_DOLLAR_arguments_2 + # + return unless RUBY_PLATFORM =~ /mswin32/ + + create_CMDTEST_foo <<'_END_' + cmd ["clines", "emb$edded1", "emb$$edded2"] do + stdout_equal [ + "emb$edded1", + "emb$$edded2", + ] + end +_END_ + + cmd_cmdtest do + stdout_equal [ + "### clines \"emb$edded1\" \"emb$$edded2\"", + ] + end + end + + #----------------------------------- + + def test_cmd_all_characters + # (but not backslash for now) + # + return unless RUBY_PLATFORM !~ /mswin32/ + + create_CMDTEST_foo <<'_END_' + all = " !\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~" + cmd ["lines.rb", all] do + stdout_equal [ + " !\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~" + ] + end +_END_ + + cmd_cmdtest do + stdout_equal <<'_END_' +### lines.rb " !\"#\$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~" +_END_ + end + end + + #----------------------------------- + + def test_BACKSLASH_character + create_CMDTEST_foo [ + "all = \" ` \"", + "cmd [\"lines.rb\", all] do", + " stdout_equal [", + " \" ` \",", + " ]", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + /### .*lines.*/, + ] + end + end + +end diff --git a/t/CMDTEST_exit_nonzero.rb b/t/CMDTEST_exit_nonzero.rb new file mode 100644 index 0000000..8ddc2bb --- /dev/null +++ b/t/CMDTEST_exit_nonzero.rb @@ -0,0 +1,63 @@ + +require "selftest_utils" + +class CMDTEST_exit_nonzero < Cmdtest::Testcase + + include SelftestUtils + + #---------------------------------------- + # 'exit_nonzero' expects the command to exit + # with a non-zero exit status + + def test_exit_nonzero_CORRECT + create_CMDTEST_foo [ + "cmd 'false.rb' do", + " exit_nonzero", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### false.rb", + ] + end + end + + #---------------------------------------- + # 'exit_nonzero' works for other non-zero + # exit statuses than 1 too + + def test_exit_nonzero_CORRECT_18 + create_CMDTEST_foo [ + "cmd 'exit.rb 18' do", + " exit_nonzero", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### exit.rb 18", + ] + end + end + + #---------------------------------------- + # 'exit_nonzero' gives an error if the exit status is 0 + + def test_exit_nonzero_INCORRECT + create_CMDTEST_foo [ + "cmd 'true.rb' do", + " exit_nonzero", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + "--- ERROR: expected nonzero exit status", + ] + exit_nonzero + end + end + +end diff --git a/t/CMDTEST_exit_status.rb b/t/CMDTEST_exit_status.rb new file mode 100644 index 0000000..6338fdd --- /dev/null +++ b/t/CMDTEST_exit_status.rb @@ -0,0 +1,74 @@ + +require "selftest_utils" + +class CMDTEST_exit_status < Cmdtest::Testcase + + include SelftestUtils + + def test_exit_status_CORRECT_0 + create_CMDTEST_foo [ + "cmd 'true.rb' do", + " exit_status 0", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + ] + end + end + + #---------------------------------------- + + def test_exit_status_INCORRECT_0 + create_CMDTEST_foo [ + "cmd 'false.rb' do", + " exit_status 0", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### false.rb", + "--- ERROR: expected 0 exit status, got 1", + ] + exit_nonzero + end + end + + #---------------------------------------- + + def test_exit_status_CORRECT_18 + create_CMDTEST_foo [ + "cmd 'exit.rb 18' do", + " exit_status 18", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### exit.rb 18", + ] + end + end + + #---------------------------------------- + + def test_exit_status_INCORRECT_18 + create_CMDTEST_foo [ + "cmd 'exit.rb 10' do", + " exit_status 18", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### exit.rb 10", + "--- ERROR: expected 18 exit status, got 10", + ] + exit_nonzero + end + end + +end diff --git a/t/CMDTEST_exit_zero.rb b/t/CMDTEST_exit_zero.rb new file mode 100644 index 0000000..a200504 --- /dev/null +++ b/t/CMDTEST_exit_zero.rb @@ -0,0 +1,58 @@ + +require "selftest_utils" + +class CMDTEST_exit_zero < Cmdtest::Testcase + + include SelftestUtils + + def test_exit_zero_CORRECT + create_CMDTEST_foo [ + "cmd 'true.rb' do", + " exit_zero", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + ] + end + end + + #---------------------------------------- + + def test_exit_zero_INCORRECT + create_CMDTEST_foo [ + "cmd 'false.rb' do", + " exit_zero", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### false.rb", + "--- ERROR: expected zero exit status, got 1", + ] + exit_nonzero + end + end + + #---------------------------------------- + + def test_exit_zero_INCORRECT_18 + create_CMDTEST_foo [ + "cmd 'exit.rb 18' do", + " exit_zero", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### exit.rb 18", + "--- ERROR: expected zero exit status, got 18", + ] + exit_nonzero + end + end + +end diff --git a/t/CMDTEST_file_equal.rb b/t/CMDTEST_file_equal.rb new file mode 100644 index 0000000..792331d --- /dev/null +++ b/t/CMDTEST_file_equal.rb @@ -0,0 +1,254 @@ + +require "selftest_utils" + +class CMDTEST_file_equal < Cmdtest::Testcase + + include SelftestUtils + + #---------------------------------------- + # 'file_equal' detects equal file content + + def test_file_equal_CORRECT_EMPTY + create_CMDTEST_foo [ + "File.open('foo', 'w') {}", + "", + "cmd 'true.rb' do", + " file_equal 'foo', ''", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + ] + end + end + + #---------------------------------------- + # 'file_equal' detects different file content + # and reports an error + + def test_file_equal_INCORRECT_EMPTY + + create_CMDTEST_foo [ + "File.open('foo', 'w') {|f| f.puts 'hello world' }", + "", + "cmd 'true.rb' do", + " file_equal 'foo', ''", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + "--- ERROR: wrong file 'foo'", + "--- actual: hello world", + "--- expect: [[empty]]", + ] + exit_nonzero + end + end + + #---------------------------------------- + # 'file_equal' detects equal file content, + # using [] argument too + + def test_file_equal_CORRECT_NO_LINES + + create_CMDTEST_foo [ + "File.open('foo', 'w') {}", + "", + "cmd 'true.rb' do", + " file_equal 'foo', []", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + ] + end + end + + #---------------------------------------- + # 'file_equal' detects different file content + # and reports an error, + # using [] argument too + + def test_file_equal_INCORRECT_NO_LINES + + create_CMDTEST_foo [ + "File.open('foo', 'w') {|f| f.puts 'hello world' }", + "", + "cmd 'true.rb' do", + " file_equal 'foo', []", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + "--- ERROR: wrong file 'foo'", + "--- actual: hello world", + "--- expect: [[empty]]", + ] + exit_nonzero + end + end + + #---------------------------------------- + # testing [] argument with one line (equal) + + def test_file_equal_CORRECT_LINE + + create_CMDTEST_foo [ + "File.open('foo', 'w') {|f| f.puts 'hello world' }", + "", + "cmd 'true.rb' do", + " file_equal 'foo', [ 'hello world' ]", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + ] + end + end + + #---------------------------------------- + # testing [] argument with one line (different) + + def test_file_equal_INCORRECT_LINE + + create_CMDTEST_foo [ + "File.open('foo', 'w') {}", + "", + "cmd 'true.rb' do", + " file_equal 'foo', [ 'hello world' ]", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + "--- ERROR: wrong file 'foo'", + "--- actual: [[empty]]", + "--- expect: hello world", + ] + exit_nonzero + end + end + + #---------------------------------------- + # testing [] argument with two lines (equal) + + def test_file_equal_CORRECT_2_LINES + + create_CMDTEST_foo [ + "File.open('foo', 'w') {|f| f.puts 'hello'; f.puts 'world' }", + "", + "cmd 'true.rb' do", + " file_equal 'foo', [ 'hello', 'world' ]", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + ] + end + end + + #---------------------------------------- + # testing [] argument with two lines (different) + + def test_file_equal_INCORRECT_2_LINES + + create_CMDTEST_foo [ + "File.open('foo', 'w') {}", + "", + "cmd 'true.rb' do", + " file_equal 'foo', [ 'hello', 'world' ]", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + "--- ERROR: wrong file 'foo'", + "--- actual: [[empty]]", + "--- expect: hello", + "--- world", + ] + exit_nonzero + end + end + + #---------------------------------------- + # non-existing file gives an error + + def test_file_equal_NON_EXISTING_FILE + + create_CMDTEST_foo [ + "cmd 'true.rb' do", + " file_equal 'foo', ''", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + "--- ERROR: no such file: 'foo'", + ] + exit_nonzero + end + end + + #---------------------------------------- + # file being a directory gives an error + + def test_file_equal_FILE_IS_DIRECTORY + + create_CMDTEST_foo [ + "Dir.mkdir 'foo'", + "", + "cmd 'true.rb' do", + " file_equal 'foo', ''", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + "--- ERROR: is a directory: 'foo'", + ] + exit_nonzero + end + end + + #---------------------------------------- + # other error also detected (recursive symlink) + + def test_file_equal_OTHER_ERROR + # + return unless RUBY_PLATFORM !~ /mswin32/ + + create_CMDTEST_foo [ + "File.symlink 'foo', 'foo'", + "", + "cmd 'true.rb' do", + " file_equal 'foo', ''", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + "--- ERROR: error reading file: 'foo'", + ] + exit_nonzero + end + end + +end diff --git a/t/CMDTEST_file_not_equal.rb b/t/CMDTEST_file_not_equal.rb new file mode 100644 index 0000000..f9a9c3f --- /dev/null +++ b/t/CMDTEST_file_not_equal.rb @@ -0,0 +1,51 @@ + +require "selftest_utils" + +class CMDTEST_file_not_equal < Cmdtest::Testcase + + include SelftestUtils + + #---------------------------------------- + # 'file_not_equal' detects different file content + + def test_file_not_equal_CORRECT + create_CMDTEST_foo [ + "File.open('foo', 'w') {|f| f.puts 'hello' }", + "", + "cmd 'true.rb' do", + " file_not_equal 'foo', ['world']", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + ] + end + end + + #----------------------------------- + # 'file_not_equal' detects equal file content + # and reports an error + + def test_file_not_equal_INCORRECT + create_CMDTEST_foo [ + "File.open('foo', 'w') {|f| f.puts 'hello' }", + "", + "cmd 'true.rb' do", + " file_not_equal 'foo', ['hello']", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + "--- ERROR: wrong file 'foo'", + "--- actual: hello", + "--- expect: hello", + ] + exit_nonzero + end + end + +end diff --git a/t/CMDTEST_ignore_file.rb b/t/CMDTEST_ignore_file.rb new file mode 100644 index 0000000..bc0c27b --- /dev/null +++ b/t/CMDTEST_ignore_file.rb @@ -0,0 +1,112 @@ + +require "selftest_utils" + +class CMDTEST_ignore_file < Cmdtest::Testcase + + include SelftestUtils + + #---------------------------------------- + # an ignored file is not counted as a created file + # even when it is actually created + + def test_ignore_file_IGNORED + create_CMDTEST_foo [ + "ignore_file 'bbb'", + "", + "cmd 'touch.rb aaa bbb' do", + " created_files 'aaa'", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### touch.rb aaa bbb", + ] + end + end + + #---------------------------------------- + # it is ok for an ignored file to not be created + + def test_ignore_file_IGNORED_NOT_CREATED + create_CMDTEST_foo [ + "ignore_file 'bbb'", + "", + "cmd 'touch.rb aaa' do", + " created_files 'aaa'", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### touch.rb aaa", + ] + end + end + + #---------------------------------------- + # 'created_files' is wrong if the file is mentioned, + # even when the file actually was created + + def test_ignore_file_AS_IF_NOT_CREATED + create_CMDTEST_foo [ + "ignore_file 'bbb'", + "", + "cmd 'touch.rb aaa bbb' do", + " created_files 'aaa', 'bbb'", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + '### touch.rb aaa bbb', + '--- ERROR: created files', + '--- actual: ["aaa"]', + '--- expect: ["aaa", "bbb"]', + ] + exit_nonzero + end + end + + #---------------------------------------- + # the ignored file can have a directory component in the filename + + def test_ignore_file_IGNORED_IN_SUBDIR + create_CMDTEST_foo [ + "ignore_file 'dir/bbb'", + "Dir.mkdir 'dir'", + "", + "cmd 'touch.rb aaa bbb dir/aaa dir/bbb' do", + " created_files 'aaa', 'bbb', 'dir/aaa'", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### touch.rb aaa bbb dir/aaa dir/bbb", + ] + end + end + + #---------------------------------------- + # the argument to 'ignore_file' is the *path* to a file, + # not just the basename. + + def test_ignore_file_PATH_MATTERS + create_CMDTEST_foo [ + "ignore_file 'bbb'", + "Dir.mkdir 'dir'", + "", + "cmd 'touch.rb aaa bbb dir/aaa dir/bbb' do", + " created_files 'aaa', 'dir/aaa', 'dir/bbb'", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### touch.rb aaa bbb dir/aaa dir/bbb", + ] + end + end + +end diff --git a/t/CMDTEST_import_file.rb b/t/CMDTEST_import_file.rb new file mode 100644 index 0000000..41158c6 --- /dev/null +++ b/t/CMDTEST_import_file.rb @@ -0,0 +1,62 @@ + +require "selftest_utils" + +class CMDTEST_import_file < Cmdtest::Testcase + + include SelftestUtils + + #---------------------------------------- + # import_file + #---------------------------------------- + + def test_import_file_DIFFERENT_DIRS + create_file "file1.txt", "This is file1.txt\n" + create_file "file2.txt", "This is file2.txt\n" + + create_CMDTEST_foo [ + "import_file 'file1.txt', 'qwerty1.txt'", + "import_file 'file2.txt', 'subdir/qwerty2.txt'", + "", + "cmd 'cat.rb qwerty1.txt subdir/qwerty2.txt' do", + " stdout_equal [", + " 'This is file1.txt',", + " 'This is file2.txt',", + " ]", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### cat.rb qwerty1.txt subdir/qwerty2.txt", + ] + end + end + + #---------------------------------------- + + def test_import_file_AFTER_CHDIR + create_file "file1.txt", "This is file1.txt\n" + create_file "file2.txt", "This is file2.txt\n" + + create_CMDTEST_foo [ + "Dir.mkdir('dir')", + "Dir.chdir('dir')", + "import_file 'file1.txt', 'qwerty1.txt'", + "import_file 'file2.txt', 'subdir/qwerty2.txt'", + "", + "cmd 'cat.rb qwerty1.txt subdir/qwerty2.txt' do", + " stdout_equal [", + " 'This is file1.txt',", + " 'This is file2.txt',", + " ]", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### cat.rb qwerty1.txt subdir/qwerty2.txt", + ] + end + end + +end diff --git a/t/CMDTEST_misc.rb b/t/CMDTEST_misc.rb new file mode 100644 index 0000000..0067ac6 --- /dev/null +++ b/t/CMDTEST_misc.rb @@ -0,0 +1,890 @@ + +require "selftest_utils" + +class CMDTEST_misc < Cmdtest::Testcase + + include SelftestUtils + + #---------------------------------------- + # Ruby script called with "ruby -S" + #---------------------------------------- + + def test_ruby_script + create_CMDTEST_foo [ + "cmd 'echo.rb this is a line' do", + " stdout_equal ['this is a line']", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### echo.rb this is a line", + ] + end + end + + #----------------------------------- + + def test_actual_false_rb_will_give_error + create_CMDTEST_foo [ + "cmd 'true.rb' do", + "end", + "", + "cmd 'false.rb' do", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + "### false.rb", + "--- ERROR: expected zero exit status, got 1", + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_actual_false_rb_will_give_error_2 + create_CMDTEST_foo [ + "cmd 'false.rb' do", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### false.rb", + "--- ERROR: expected zero exit status, got 1", + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_another_non_zero_exit_will_give_error + create_CMDTEST_foo [ + "cmd 'exit.rb 18' do", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### exit.rb 18", + "--- ERROR: expected zero exit status, got 18", + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_actual_STDOUT_will_give_error + create_CMDTEST_foo [ + "cmd 'echo.rb a line on stdout' do", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### echo.rb a line on stdout", + "--- ERROR: wrong stdout", + "--- actual: a line on stdout", + "--- expect: [[empty]]", + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_actual_STDERR_will_give_error + create_CMDTEST_foo [ + "cmd 'echo.rb a line on stderr 1>&2' do", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### echo.rb a line on stderr 1>&2", + "--- ERROR: wrong stderr", + "--- actual: a line on stderr", + "--- expect: [[empty]]", + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_existing_files_is_OK + create_CMDTEST_foo [ + "File.open('before1', 'w') {}", + "File.open('before2', 'w') {}", + "cmd 'true.rb' do", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + ] + end + end + + #----------------------------------- + + def test_actual_created_file_will_give_error + create_CMDTEST_foo [ + "cmd 'echo.rb content > a-new-file' do", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### echo.rb content > a-new-file", + "--- ERROR: created files", + '--- actual: ["a-new-file"]', + "--- expect: []", + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_several_actual_created_files_will_give_error + create_CMDTEST_foo [ + "File.open('before1', 'w') {}", + "File.open('before2', 'w') {}", + "cmd 'echo.rb x > a && echo.rb x > b' do", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### echo.rb x > a && echo.rb x > b", + "--- ERROR: created files", + '--- actual: ["a", "b"]', + "--- expect: []", + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_actual_removed_file_will_give_error + create_CMDTEST_foo [ + "File.open('before', 'w') {}", + "cmd 'rm.rb before' do", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### rm.rb before", + "--- ERROR: removed files", + '--- actual: ["before"]', + "--- expect: []", + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_several_actual_removed_files_will_give_error + create_CMDTEST_foo [ + "File.open('before1', 'w') {}", + "File.open('before2', 'w') {}", + "File.open('before3', 'w') {}", + "cmd 'rm.rb before1 before2' do", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### rm.rb before1 before2", + "--- ERROR: removed files", + '--- actual: ["before1", "before2"]', + "--- expect: []", + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_actual_changed_files_will_give_error + # NOTE: order of writing/testing is important below + create_CMDTEST_foo <<'_END_' + File.open('changed1', 'w') {} + File.open('changed2', 'w') {} + + File.open('script.rb', 'w') do |f| + f.puts 't1 = File.mtime("changed2")' + f.puts 'while File.mtime("changed2") == t1' + f.puts ' File.open("changed2", "w") {|f| f.puts 111 }' + f.puts ' File.open("changed1", "w") {|f| f.puts 111 }' + f.puts 'end' + end + + cmd 'ruby script.rb' do + end +_END_ + + cmd_cmdtest do + stdout_equal [ + "### ruby script.rb", + "--- ERROR: changed files", + '--- actual: ["changed1", "changed2"]', + "--- expect: []", + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_mix_of_actual_created_removed_files_will_give_error + create_CMDTEST_foo [ + "File.open('before1', 'w') {}", + "File.open('before2', 'w') {}", + "File.open('before3', 'w') {}", + "cmd 'rm.rb before1 before2 && echo.rb x > a && echo.rb x > b' do", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### rm.rb before1 before2 && echo.rb x > a && echo.rb x > b", + "--- ERROR: created files", + '--- actual: ["a", "b"]', + "--- expect: []", + "--- ERROR: removed files", + '--- actual: ["before1", "before2"]', + "--- expect: []", + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_mix_of_all_errros + create_CMDTEST_foo <<'_END_' + File.open('before1', 'w') {} + File.open('before2', 'w') {} + File.open('before3', 'w') {} + File.open('script.rb', 'w') do |f| + f.puts 'File.unlink "before1"' + f.puts 'File.unlink "before2"' + f.puts 'File.open("a", "w") {}' + f.puts 'File.open("b", "w") {}' + f.puts 'STDOUT.puts [11,22,33]' + f.puts 'STDERR.puts [44,55,66]' + f.puts 'exit 39' + end + cmd 'ruby script.rb' do + end +_END_ + + cmd_cmdtest do + stdout_equal [ + "### ruby script.rb", + "--- ERROR: expected zero exit status, got 39", + "--- ERROR: wrong stdout", + "--- actual: 11", + "--- 22", + "--- 33", + "--- expect: [[empty]]", + "--- ERROR: wrong stderr", + "--- actual: 44", + "--- 55", + "--- 66", + "--- expect: [[empty]]", + "--- ERROR: created files", + '--- actual: ["a", "b"]', + "--- expect: []", + "--- ERROR: removed files", + '--- actual: ["before1", "before2"]', + "--- expect: []", + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_removed_files + create_CMDTEST_foo [ + "File.open('file1', 'w') {}", + "File.open('file2', 'w') {}", + "", + "cmd 'rm.rb file1' do", + " comment 'removed_files'", + " removed_files 'file1'", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### removed_files", + ] + end + end + + #----------------------------------- + + def test_FAILED_removed_files + create_CMDTEST_foo [ + "File.open('file1', 'w') {}", + "File.open('file2', 'w') {}", + "", + "cmd 'true.rb' do", + " comment 'FAILED removed_files'", + " removed_files 'file1'", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### FAILED removed_files", + "--- ERROR: removed files", + "--- actual: []", + '--- expect: ["file1"]', + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_changed_files + create_CMDTEST_foo [ + "File.open('file1', 'w') {}", + "File.open('file2', 'w') {}", + "", + "cmd 'sleep.rb 1 && touch.rb file1' do", + " comment 'changed_files'", + " changed_files 'file1'", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### changed_files", + ] + end + end + + #----------------------------------- + + def test_FAILED_changed_files + + create_CMDTEST_foo [ + "File.open('file1', 'w') {}", + "File.open('file2', 'w') {}", + "", + "cmd 'true.rb' do", + " comment 'FAILED changed_files'", + " changed_files 'file1'", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### FAILED changed_files", + "--- ERROR: changed files", + "--- actual: []", + '--- expect: ["file1"]', + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_created_files + create_CMDTEST_foo [ + "File.open('file1', 'w') {}", + "File.open('file2', 'w') {}", + "", + "cmd 'touch.rb file3' do", + " comment 'created_files'", + " created_files 'file3'", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### created_files", + ] + end + end + + #----------------------------------- + + def test_FAILED_created_files + create_CMDTEST_foo [ + "File.open('file1', 'w') {}", + "File.open('file2', 'w') {}", + "", + "cmd 'true.rb' do", + " comment 'FAILED created_files'", + " created_files 'file3'", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### FAILED created_files", + "--- ERROR: created files", + "--- actual: []", + '--- expect: ["file3"]', + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_with_comment + create_CMDTEST_foo [ + "cmd 'true.rb' do", + " comment 'this-is-the-comment'", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### this-is-the-comment", + ] + end + end + + #----------------------------------- + + def test_exit_nonzero + create_CMDTEST_foo [ + "cmd 'exit.rb 33' do", + " comment 'exit_nonzero'", + " exit_nonzero", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### exit_nonzero", + ] + end + end + + #----------------------------------- + + def test_FAILING_exit_nonzero + create_CMDTEST_foo [ + "cmd 'exit.rb 0' do", + " comment 'failing exit_nonzero'", + " exit_nonzero", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### failing exit_nonzero", + "--- ERROR: expected nonzero exit status", + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_exit_status + create_CMDTEST_foo [ + "cmd 'exit.rb 33' do", + " comment 'exit_status'", + " exit_status 33", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### exit_status", + ] + end + end + + #----------------------------------- + + def test_FAILING_exit_status + create_CMDTEST_foo [ + "cmd 'exit.rb 44' do", + " comment 'failing exit_status'", + " exit_status 33", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### failing exit_status", + "--- ERROR: expected 33 exit status, got 44", + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_stdout_equal_ONE_LINE + create_CMDTEST_foo [ + "cmd 'lines.rb 11' do", + " comment 'stdout_equal'", + " stdout_equal '11\n'", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### stdout_equal", + ] + end + end + + #----------------------------------- + + def test_FAILING_stdout_equal_ONE_LINE + create_CMDTEST_foo [ + "cmd 'lines.rb 22' do", + " comment 'stdout_equal'", + " stdout_equal '11\n'", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### stdout_equal", + "--- ERROR: wrong stdout", + "--- actual: 22", + "--- expect: 11", + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_stdout_equal_TWO_LINES + create_CMDTEST_foo [ + "cmd 'lines.rb 11 22' do", + " comment 'stdout_equal'", + " stdout_equal '11\n22\n'", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### stdout_equal", + ] + end + end + + #----------------------------------- + + def test_FAILING_stdout_equal_TWO_LINES + create_CMDTEST_foo [ + "cmd 'lines.rb 33 44' do", + " comment 'stdout_equal'", + " stdout_equal '11\n22\n'", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### stdout_equal", + "--- ERROR: wrong stdout", + "--- actual: 33", + "--- 44", + "--- expect: 11", + "--- 22", + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_stdout_equal_ARR_TWO_LINES + create_CMDTEST_foo [ + "cmd 'lines.rb 11 22' do", + " comment 'stdout_equal'", + " stdout_equal ['11', '22']", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### stdout_equal", + ] + end + end + + #----------------------------------- + + def test_FAILING_stdout_equal_ARR_TWO_LINES + create_CMDTEST_foo [ + "cmd 'lines.rb 33 44' do", + " comment 'stdout_equal'", + " stdout_equal ['11', '22']", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### stdout_equal", + "--- ERROR: wrong stdout", + "--- actual: 33", + "--- 44", + "--- expect: 11", + "--- 22", + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_FAILING_stdout_equal_ARR_DIFFERENT_NR_LINES + create_CMDTEST_foo [ + "cmd 'lines.rb 11 22 33' do", + " comment 'stdout_equal'", + " stdout_equal ['11', '22']", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### stdout_equal", + "--- ERROR: wrong stdout", + "--- actual: 11", + "--- 22", + "--- 33", + "--- expect: 11", + "--- 22", + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_stdout_equal_REGEXP_ARGUMENT + create_CMDTEST_foo [ + "cmd 'lines.rb 11 22' do", + " comment 'stdout_equal'", + " stdout_equal /^22$/", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### stdout_equal", + ] + end + end + + #----------------------------------- + + def test_stdout_equal_TWICE_REGEXP_ARGUMENT + create_CMDTEST_foo [ + "cmd 'lines.rb 11 22' do", + " comment 'stdout_equal'", + " stdout_equal /^22$/", + " stdout_equal /^11$/", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### stdout_equal", + ] + end + end + + #----------------------------------- + + def test_FAILING_first_stdout_equal_twice_regexp_argument + create_CMDTEST_foo [ + "cmd 'lines.rb 99 22' do", + " comment 'stdout_equal'", + " stdout_equal /^22$/", + " stdout_equal /^11$/", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### stdout_equal", + "--- ERROR: wrong stdout", + "--- actual: 99", + "--- 22", + "--- expect: (?-mix:^11$)", + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_FAILING_second_stdout_equal_twice_regexp_argument + create_CMDTEST_foo [ + "cmd 'lines.rb 11 99' do", + " comment 'stdout_equal'", + " stdout_equal /^22$/", + " stdout_equal /^11$/", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### stdout_equal", + "--- ERROR: wrong stdout", + "--- actual: 11", + "--- 99", + "--- expect: (?-mix:^22$)", + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_FAILING_stdout_equal_REGEXP_ARGUMENT + create_CMDTEST_foo [ + "cmd 'lines.rb 11 22' do", + " comment 'stdout_equal'", + " stdout_equal /^\d+ \d+$/", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### stdout_equal", + "--- ERROR: wrong stdout", + "--- actual: 11", + "--- 22", + "--- expect: (?-mix:^\d+ \d+$)", + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_stdout_equal_ARR_REGEXP_ARGUMENT + create_CMDTEST_foo [ + "cmd 'lines.rb 11 22' do", + " comment 'stdout_equal'", + " stdout_equal ['11', /^22$/]", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### stdout_equal", + ] + end + end + + #----------------------------------- + + def test_stdout_equal_ARR_REGEXP_ARGUMENT_II + create_CMDTEST_foo [ + "cmd 'lines.rb 11 22' do", + " comment 'stdout_equal'", + " stdout_equal ['11', /^\\d+$/]", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### stdout_equal", + ] + end + end + + #----------------------------------- + + def test_FAILING_stdout_equal_ARR_REGEXP_ARGUMENT + create_CMDTEST_foo [ + "cmd 'lines.rb 11 22' do", + " comment 'stdout_equal'", + " stdout_equal ['11', /^\d+ \d+$/]", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### stdout_equal", + "--- ERROR: wrong stdout", + "--- actual: 11", + "--- 22", + "--- expect: 11", + "--- (?-mix:^\d+ \d+$)", + ] + exit_nonzero + end + end + + #====================================================================== + + #----------------------------------- + # symlinks in tree -- should work + # TODO: this test should be improved to actually trigger the difference + # between lstat/stat in "_update_hardlinks". + # + # REQUIRE: RUBY_PLATFORM !~ /mswin32/ + + def test_symlinks_in_tree + create_CMDTEST_foo [ + "File.symlink 'non-existing', 'non-existing-link'", + "", + "File.open('existing', 'w') {}", + "File.symlink 'existing', 'existing-link'", + "", + "cmd 'true.rb' do", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + ] + end + end + + #----------------------------------- + + def test_file_with_mtime_in_future + create_CMDTEST_foo [ + "File.open('future-file', 'w') {}", + "future = Time.now + 86400", + "File.utime future, future, 'future-file'", + "", + "cmd 'true.rb' do", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + ] + end + end + + +end + diff --git a/t/CMDTEST_simple.rb b/t/CMDTEST_simple.rb new file mode 100644 index 0000000..e04fbe6 --- /dev/null +++ b/t/CMDTEST_simple.rb @@ -0,0 +1,553 @@ + +require "selftest_utils" + +class CMDTEST_simple < Cmdtest::Testcase + + include SelftestUtils + + #----------------------------------- + + def test_try_to_run_non_existing_command_LINUX + # + return unless RUBY_PLATFORM !~ /mswin32/ + + create_CMDTEST_foo [ + 'cmd "non-existing" do', + ' exit_nonzero', + ' stderr_equal /non-existing: .*not found/', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### non-existing', + ] + end + end + + #----------------------------------- + + def test_try_to_run_non_existing_command_WINDOWS + # + return unless RUBY_PLATFORM =~ /mswin32/ + + create_CMDTEST_foo [ + 'cmd "non-existing" do', + ' exit_nonzero', + ' stderr_equal [', + ' /non-existing.*not recognized/,', + ' /program or batch file/,', + ' ]', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### non-existing', + ] + end + end + + #----------------------------------- + + def test_FAILING_try_to_run_non_existing_command_LINUX + # + return unless RUBY_PLATFORM !~ /mswin32/ + + create_CMDTEST_foo [ + 'cmd "non-existing" do', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### non-existing', + '--- ERROR: expected zero exit status, got 127', + '--- ERROR: wrong stderr', + /--- actual:.*non-existing: .*not found/, + '--- expect: [[empty]]', + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_FAILING_try_to_run_non_existing_command_WIN32 + # + return unless RUBY_PLATFORM =~ /mswin32/ + + create_CMDTEST_foo [ + 'cmd "non-existing" do', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### non-existing', + '--- ERROR: expected zero exit status, got 1', + '--- ERROR: wrong stderr', + /--- actual:.*non-existing.*not recognized/, + /--- .*program or batch file/, + '--- expect: [[empty]]', + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_true_rb_is_archetypic_command__zero_exit_status__no_output + create_CMDTEST_foo [ + 'cmd "true.rb" do', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### true.rb', + ] + end + end + + #----------------------------------- + + def test_true___explicit_exit_zero + create_CMDTEST_foo [ + 'cmd "true.rb" do', + ' exit_zero', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### true.rb', + ] + end + end + + #----------------------------------- + + def test_true___incorrect_exit_nonzero + create_CMDTEST_foo [ + 'cmd "true.rb" do', + ' exit_nonzero', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### true.rb', + '--- ERROR: expected nonzero exit status', + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_true___incorrect_exit_status + create_CMDTEST_foo [ + 'cmd "true.rb" do', + ' exit_status 18', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### true.rb', + '--- ERROR: expected 18 exit status, got 0', + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_true___correct_exit_status + create_CMDTEST_foo [ + 'cmd "true.rb" do', + ' exit_status 0', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### true.rb', + ] + end + end + + #----------------------------------- + + def test_true___incorrect_stdout + create_CMDTEST_foo [ + 'cmd "true.rb" do', + ' stdout_equal ["hello"]', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### true.rb', + '--- ERROR: wrong stdout', + '--- actual: [[empty]]', + '--- expect: hello', + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_true___correct_stdout + create_CMDTEST_foo [ + 'cmd "true.rb" do', + ' stdout_equal []', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### true.rb', + ] + end + end + + #----------------------------------- + + def test_true___incorrect_stderr + create_CMDTEST_foo [ + 'cmd "true.rb" do', + ' stderr_equal ["hello"]', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### true.rb', + '--- ERROR: wrong stderr', + '--- actual: [[empty]]', + '--- expect: hello', + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_true___correct_stderr + create_CMDTEST_foo [ + 'cmd "true.rb" do', + ' stderr_equal []', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### true.rb', + ] + end + end + + #----------------------------------- + + def test_true___incorrect_created_files_! + create_CMDTEST_foo [ + 'cmd "true.rb" do', + ' created_files "foo"', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### true.rb', + '--- ERROR: created files', + '--- actual: []', + '--- expect: ["foo"]', + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_true___incorrect_created_files_2 + create_CMDTEST_foo [ + 'cmd "true.rb" do', + ' created_files "foo", "bar"', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### true.rb', + '--- ERROR: created files', + '--- actual: []', + '--- expect: ["bar", "foo"]', + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_true___correct_created_files + create_CMDTEST_foo [ + 'cmd "true.rb" do', + ' created_files', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### true.rb', + ] + end + end + + #----------------------------------- + + def test_true___incorrect_changed_files_1 + create_CMDTEST_foo [ + 'cmd "true.rb" do', + ' changed_files "foo"', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### true.rb', + '--- ERROR: changed files', + '--- actual: []', + '--- expect: ["foo"]', + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_true___incorrect_changed_files_2 + create_CMDTEST_foo [ + 'cmd "true.rb" do', + ' changed_files "foo", "bar"', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### true.rb', + '--- ERROR: changed files', + '--- actual: []', + '--- expect: ["bar", "foo"]', + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_true___correct_changed_files + create_CMDTEST_foo [ + 'cmd "true.rb" do', + ' changed_files', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### true.rb', + ] + end + end + + #----------------------------------- + + def test_true___incorrect_removed_files_1 + create_CMDTEST_foo [ + 'cmd "true.rb" do', + ' removed_files "foo"', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### true.rb', + '--- ERROR: removed files', + '--- actual: []', + '--- expect: ["foo"]', + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_true___incorrect_removed_files_2 + create_CMDTEST_foo [ + 'cmd "true.rb" do', + ' removed_files "foo", "bar"', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### true.rb', + '--- ERROR: removed files', + '--- actual: []', + '--- expect: ["bar", "foo"]', + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_true___correct_removed_files + create_CMDTEST_foo [ + 'cmd "true.rb" do', + ' removed_files', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### true.rb', + ] + end + end + + #====================================================================== + # test - without assertions + + #----------------------------------- + + def test_without_assertions____correct + create_CMDTEST_foo [ + 'cmd "true.rb" do', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### true.rb', + ] + end + end + + #----------------------------------- + + def test_without_assertions____incorrect_exit_status + create_CMDTEST_foo [ + 'cmd "false.rb" do', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### false.rb', + '--- ERROR: expected zero exit status, got 1', + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_without_assertions____incorrect_stdout + create_CMDTEST_foo [ + 'cmd "echo.rb hello" do', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### echo.rb hello', + '--- ERROR: wrong stdout', + '--- actual: hello', + '--- expect: [[empty]]', + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_without_assertions____incorrect_stderr + create_CMDTEST_foo [ + 'cmd "echo.rb hello >&2" do', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### echo.rb hello >&2', + '--- ERROR: wrong stderr', + '--- actual: hello', + '--- expect: [[empty]]', + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_without_assertions____incorrect_created_files + create_CMDTEST_foo [ + 'cmd "touch.rb new_file" do', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### touch.rb new_file', + '--- ERROR: created files', + '--- actual: ["new_file"]', + '--- expect: []', + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_without_assertions____incorrect_changed_files + create_CMDTEST_foo [ + 'touch_file "changed_file"', + 'cmd "echo.rb ... >> changed_file" do', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### echo.rb ... >> changed_file', + '--- ERROR: changed files', + '--- actual: ["changed_file"]', + '--- expect: []', + ] + exit_nonzero + end + end + + #----------------------------------- + + def test_without_assertions____incorrect_removed_files + create_CMDTEST_foo [ + 'touch_file "removed_file"', + 'cmd "rm.rb removed_file" do', + 'end', + ] + + cmd_cmdtest do + stdout_equal [ + '### rm.rb removed_file', + '--- ERROR: removed files', + '--- actual: ["removed_file"]', + '--- expect: []', + ] + exit_nonzero + end + end + +end diff --git a/t/CMDTEST_stdxxx_equal.rb b/t/CMDTEST_stdxxx_equal.rb new file mode 100644 index 0000000..2a4cec3 --- /dev/null +++ b/t/CMDTEST_stdxxx_equal.rb @@ -0,0 +1,355 @@ + +require "selftest_utils" + +class CMDTEST_stdxxx_equal < Cmdtest::Testcase + + include SelftestUtils + + #======================================== + # Using "define_method" to avoid duplicating definitions of + # stderr/stdout methods. The follwing section tests: + # + # stderr_equal + # stderr_not_equal + # stdout_equal + # stdout_not_equal + # + + def self._define_stdxxx_methods(stdxxx) + + #---------------------------------------- + # stdxxx_equal + #---------------------------------------- + + ## methods: test_stdout_equal_CORRECT_EMPTY test_stderr_equal_CORRECT_EMPTY + + define_method("test_#{stdxxx}_equal_CORRECT_EMPTY") do + create_CMDTEST_foo [ + "cmd 'true.rb' do", + " #{stdxxx}_equal ''", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + ] + end + end + + #---------------------------------------- + ## methods: test_stdout_equal_INCORRECT_EMPTY test_stderr_equal_INCORRECT_EMPTY + + define_method("test_#{stdxxx}_equal_INCORRECT_EMPTY") do + create_CMDTEST_foo [ + "cmd 'echo-#{stdxxx}.rb hello world' do", + " #{stdxxx}_equal ''", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### echo-#{stdxxx}.rb hello world", + "--- ERROR: wrong #{stdxxx}", + "--- actual: hello world", + "--- expect: [[empty]]", + ] + exit_nonzero + end + end + + #---------------------------------------- + + ## methods: test_stdout_equal_CORRECT_NO_LINES test_stderr_equal_CORRECT_NO_LINES + + define_method("test_#{stdxxx}_equal_CORRECT_NO_LINES") do + create_CMDTEST_foo [ + "cmd 'true.rb' do", + " #{stdxxx}_equal []", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + ] + end + end + + #---------------------------------------- + + ## methods: test_stdout_equal_INCORRECT_NO_LINES test_stderr_equal_INCORRECT_NO_LINES + + define_method("test_#{stdxxx}_equal_INCORRECT_NO_LINES") do + create_CMDTEST_foo [ + "cmd 'echo-#{stdxxx}.rb hello world' do", + " #{stdxxx}_equal []", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### echo-#{stdxxx}.rb hello world", + "--- ERROR: wrong #{stdxxx}", + "--- actual: hello world", + "--- expect: [[empty]]", + ] + exit_nonzero + end + end + + #---------------------------------------- + + ## methods: test_stdout_equal_CORRECT_1_LINE test_stderr_equal_CORRECT_1_LINE + + define_method("test_#{stdxxx}_equal_CORRECT_1_LINE") do + create_CMDTEST_foo [ + "cmd 'echo-#{stdxxx}.rb hello world' do", + " #{stdxxx}_equal [ 'hello world' ]", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### echo-#{stdxxx}.rb hello world", + ] + end + end + + #---------------------------------------- + + ## methods: test_stdout_equal_INCORRECT_1_LINE test_stderr_equal_INCORRECT_1_LINE + + define_method("test_#{stdxxx}_equal_INCORRECT_1_LINE") do + create_CMDTEST_foo [ + "cmd 'true.rb' do", + " #{stdxxx}_equal [ 'hello world' ]", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + "--- ERROR: wrong #{stdxxx}", + "--- actual: [[empty]]", + "--- expect: hello world", + ] + exit_nonzero + end + end + + #---------------------------------------- + + ## methods: test_stdout_equal_CORRECT_2_LINES test_stderr_equal_CORRECT_2_LINES + + define_method("test_#{stdxxx}_equal_CORRECT_2_LINES") do + + create_CMDTEST_foo [ + "cmd 'echo-#{stdxxx}.rb hello && echo-#{stdxxx}.rb world' do", + " #{stdxxx}_equal [ 'hello', 'world' ]", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### echo-#{stdxxx}.rb hello && echo-#{stdxxx}.rb world", + ] + end + end + + #---------------------------------------- + + ## methods: test_stdout_equal_INCORRECT_2_LINES test_stderr_equal_INCORRECT_2_LINES + + define_method("test_#{stdxxx}_equal_INCORRECT_2_LINES") do + + create_CMDTEST_foo [ + "cmd 'true.rb' do", + " #{stdxxx}_equal [ 'hello', 'world' ]", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + "--- ERROR: wrong #{stdxxx}", + "--- actual: [[empty]]", + "--- expect: hello", + "--- world", + ] + exit_nonzero + end + end + + #---------------------------------------- + # stdxxx_not_equal + #---------------------------------------- + + ## methods: test_stdout_not_equal_CORRECT_EMPTY test_stderr_not_equal_CORRECT_EMPTY + + define_method("test_#{stdxxx}_not_equal_CORRECT_EMPTY") do + create_CMDTEST_foo [ + "cmd 'echo-#{stdxxx}.rb hello' do", + " #{stdxxx}_not_equal ''", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### echo-#{stdxxx}.rb hello", + ] + end + end + + #---------------------------------------- + + ## methods: test_stdout_not_equal_INCORRECT_EMPTY test_stderr_not_equal_INCORRECT_EMPTY + + define_method("test_#{stdxxx}_not_equal_INCORRECT_EMPTY") do + create_CMDTEST_foo [ + "cmd 'true.rb' do", + " #{stdxxx}_not_equal ''", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + "--- ERROR: wrong #{stdxxx}", + "--- actual: [[empty]]", + "--- expect: [[empty]]", + ] + exit_nonzero + end + end + + #---------------------------------------- + + ## methods: test_stdout_not_equal_CORRECT_NO_LINES test_stderr_not_equal_CORRECT_NO_LINES + + define_method("test_#{stdxxx}_not_equal_CORRECT_NO_LINES") do + create_CMDTEST_foo [ + "cmd 'echo-#{stdxxx}.rb hello' do", + " #{stdxxx}_not_equal []", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### echo-#{stdxxx}.rb hello", + ] + end + end + + #---------------------------------------- + ## methods: test_stdout_not_equal_INCORRECT_NO_LINES test_stderr_not_equal_INCORRECT_NO_LINES + + define_method("test_#{stdxxx}_not_equal_INCORRECT_NO_LINES") do + create_CMDTEST_foo [ + "cmd 'true.rb' do", + " #{stdxxx}_not_equal []", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### true.rb", + "--- ERROR: wrong #{stdxxx}", + "--- actual: [[empty]]", + "--- expect: [[empty]]", + ] + exit_nonzero + end + end + + #---------------------------------------- + + ## methods: test_stdout_not_equal_CORRECT_1_LINE test_stderr_not_equal_CORRECT_1_LINE + + define_method("test_#{stdxxx}_not_equal_CORRECT_1_LINE") do + create_CMDTEST_foo [ + "cmd 'echo-#{stdxxx}.rb not hello world' do", + " #{stdxxx}_not_equal [ 'hello world' ]", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### echo-#{stdxxx}.rb not hello world", + ] + end + end + + #---------------------------------------- + + ## methods: test_stdout_not_equal_INCORRECT_1_LINE test_stderr_not_equal_INCORRECT_1_LINE + + define_method("test_#{stdxxx}_not_equal_INCORRECT_1_LINE") do + create_CMDTEST_foo [ + "cmd 'echo-#{stdxxx}.rb hello world' do", + " #{stdxxx}_not_equal [ 'hello world' ]", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### echo-#{stdxxx}.rb hello world", + "--- ERROR: wrong #{stdxxx}", + "--- actual: hello world", + "--- expect: hello world", + ] + exit_nonzero + end + end + + #---------------------------------------- + + ## methods: test_stdout_not_equal_CORRECT_2_LINES test_stderr_not_equal_CORRECT_2_LINES + + define_method("test_#{stdxxx}_not_equal_CORRECT_2_LINES") do + create_CMDTEST_foo [ + "cmd 'echo-#{stdxxx}.rb hello world' do", + " #{stdxxx}_not_equal [ 'hello', 'world' ]", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### echo-#{stdxxx}.rb hello world", + ] + end + end + + #---------------------------------------- + + ## methods: test_stdout_not_equal_INCORRECT_2_LINES test_stderr_not_equal_INCORRECT_2_LINES + + define_method("test_#{stdxxx}_not_equal_INCORRECT_2_LINES") do + create_CMDTEST_foo [ + "cmd 'echo-#{stdxxx}.rb hello && echo-#{stdxxx}.rb world' do", + " #{stdxxx}_not_equal [ 'hello', 'world' ]", + "end", + ] + + cmd_cmdtest do + stdout_equal [ + "### echo-#{stdxxx}.rb hello && echo-#{stdxxx}.rb world", + "--- ERROR: wrong #{stdxxx}", + "--- actual: hello", + "--- world", + "--- expect: hello", + "--- world", + ] + exit_nonzero + end + end + end # _define_stdxxx_methods + + #---------------------------------------- + + for stdxxx in ["stderr", "stdout"] + _define_stdxxx_methods(stdxxx) + end + +end + diff --git a/t/selftest_utils.rb b/t/selftest_utils.rb new file mode 100644 index 0000000..355ef1f --- /dev/null +++ b/t/selftest_utils.rb @@ -0,0 +1,46 @@ + + +module SelftestUtils + + TOP = Dir.pwd + BIN = File.join(TOP, "t/bin") + + if RUBY_PLATFORM =~ /mswin/ + PLATFORM_BIN = File.join(TOP, "t/bin/windows") + else + PLATFORM_BIN = File.join(TOP, "t/bin/unix") + end + + def setup + ignore_file ".cmdtest-filter" + ignore_file "tmp-cmdtest-2/" + ignore_file "tmp-cmdtest-2/TIMESTAMP" + ignore_file "tmp-cmdtest-2/tmp-command.sh" + ignore_file "tmp-cmdtest-2/tmp-stderr.log" + ignore_file "tmp-cmdtest-2/tmp-stdout.log" + ignore_file "tmp-cmdtest-2/workdir/" + end + + def create_CMDTEST_foo(lines) + create_file "CMDTEST_foo.rb", [ + "class CMDTEST_foo < Cmdtest::Testcase", + " def setup", + " prepend_path #{BIN.inspect}", + " prepend_path #{PLATFORM_BIN.inspect}", + " end", + "", + " def test_foo", + lines, + " end", + "end", + ] + end + + def cmd_cmdtest(*args) + cmd("ruby #{TOP}/bin/cmdtest.rb --quiet", *args) do + comment "running local cmdtest" + yield + end + end + +end