From 6a7bdbdfe402ef474b5fa806067b83ec1f7c2b7a Mon Sep 17 00:00:00 2001
From: Johan Holmberg <holmberg556@gmail.com>
Date: Sat, 5 Nov 2016 00:26:23 +0100
Subject: [PATCH] make tests more Windows-friendly

by using Ruby helper scripts instead fo relying
on the existence of Unix commands
---
 t/CMDTEST_chdir.rb  | 18 +++++++++---------
 t/CMDTEST_simple.rb | 24 ++++++++++++------------
 t/bin/diff.rb       | 10 ++++++++++
 t/bin/echo_path.rb  |  3 +++
 t/bin/echo_pwd.rb   |  3 +++
 t/bin/find_files.rb | 12 ++++++++++++
 6 files changed, 49 insertions(+), 21 deletions(-)
 create mode 100755 t/bin/diff.rb
 create mode 100755 t/bin/echo_path.rb
 create mode 100755 t/bin/echo_pwd.rb
 create mode 100755 t/bin/find_files.rb

diff --git a/t/CMDTEST_chdir.rb b/t/CMDTEST_chdir.rb
index 1963386..99cf42f 100644
--- a/t/CMDTEST_chdir.rb
+++ b/t/CMDTEST_chdir.rb
@@ -21,7 +21,7 @@ class CMDTEST_chdir < Cmdtest::Testcase
   def test_chdir_NONE
     create_CMDTEST_foo [
       "create_file 'SUBDIR/.flagfile', ''",
-      "system 'pwd'",
+      "puts Dir.pwd",
     ]
     cmd_cmdtest do
       stdout_equal /^\/.*\/top\/work$/
@@ -34,7 +34,7 @@ class CMDTEST_chdir < Cmdtest::Testcase
     create_CMDTEST_foo [
       "create_file 'SUBDIR/.flagfile', ''",
       "chdir 'SUBDIR'",
-      "system 'pwd'",
+      "puts Dir.pwd",
     ]
     cmd_cmdtest do
       stdout_equal /^\/.*\/top\/work\/SUBDIR$/
@@ -47,7 +47,7 @@ class CMDTEST_chdir < Cmdtest::Testcase
     create_CMDTEST_foo [
       "create_file 'SUBDIR/.flagfile', ''",
       "Dir.chdir 'SUBDIR'",
-      "system 'pwd'",
+      "puts Dir.pwd",
     ]
     cmd_cmdtest do
       stdout_equal /^\/.*\/top\/work\/SUBDIR$/
@@ -59,13 +59,13 @@ class CMDTEST_chdir < Cmdtest::Testcase
   def test_chdir_NONE_cmd
     create_CMDTEST_foo [
       "create_file 'SUBDIR/.flagfile', ''",
-      "cmd 'echo PWD=$(pwd)' do",
+      "cmd 'echo_pwd.rb' do",
       "    stdout_equal /^PWD=\\/.*\\/top\\/work$/",
       "end",
     ]
     cmd_cmdtest do
       stdout_equal [
-        "### echo PWD=$(pwd)",
+        "### echo_pwd.rb",
       ]
     end
   end
@@ -75,13 +75,13 @@ class CMDTEST_chdir < Cmdtest::Testcase
     create_CMDTEST_foo [
       "create_file 'SUBDIR/.flagfile', ''",
       "chdir 'SUBDIR'",
-      "cmd 'echo PWD=$(pwd)' do",
+      "cmd 'echo_pwd.rb' do",
       "    stdout_equal /^PWD=\\/.*\\/top\\/work\\/SUBDIR$/",
       "end",
     ]
     cmd_cmdtest do
       stdout_equal [
-        "### echo PWD=$(pwd)",
+        "### echo_pwd.rb",
       ]
     end
   end
@@ -110,7 +110,7 @@ class CMDTEST_chdir < Cmdtest::Testcase
       "Dir.chdir 'SUBDIR'",
       "create_file 'f2.txt', ''",
       "Dir.chdir '..'",
-      "cmd 'find . -type f | sort' do",
+      "cmd 'find_files.rb' do",
       "    stdout_equal [",
       "        './SUBDIR/f0.txt',",
       "        './SUBDIR/f2.txt',",
@@ -120,7 +120,7 @@ class CMDTEST_chdir < Cmdtest::Testcase
     ]
     cmd_cmdtest do
       stdout_equal [
-        "### find . -type f | sort",
+        "### find_files.rb",
       ]
     end
   end
diff --git a/t/CMDTEST_simple.rb b/t/CMDTEST_simple.rb
index 5093213..45bbb90 100644
--- a/t/CMDTEST_simple.rb
+++ b/t/CMDTEST_simple.rb
@@ -4,39 +4,39 @@ require "selftest_utils"
 class CMDTEST_simple < Cmdtest::Testcase
 
   include SelftestUtils
-    
+
   #-----------------------------------
 
   def test_get_path
     create_CMDTEST_foo [
       'old = get_path()',
-      'cmd "echo $PATH > old.path" do',
+      'cmd "echo_path.rb > old.path" do',
       '    created_files "old.path"',
       'end',
       'set_path("extra/dir", *old)',
-      'cmd "echo $PATH > new.path" do',
+      'cmd "echo_path.rb > new.path" do',
       '    created_files "new.path"',
       'end',
-      'cmd "diff -q old.path new.path" do',
+      'cmd "diff.rb old.path new.path" do',
       '    exit_nonzero',
       '    stdout_equal /differ/',
       'end',
 
       'set_path(*old)',
-      'cmd "echo $PATH > restored.path" do',
+      'cmd "echo_path.rb > restored.path" do',
       '    created_files "restored.path"',
       'end',
-      'cmd "diff -q old.path restored.path" do',
+      'cmd "diff.rb old.path restored.path" do',
       'end',
     ]
 
     cmd_cmdtest do
       stdout_equal [
-        "### echo $PATH > old.path",
-        "### echo $PATH > new.path",
-        "### diff -q old.path new.path",
-        "### echo $PATH > restored.path",
-        "### diff -q old.path restored.path",
+        "### echo_path.rb > old.path",
+        "### echo_path.rb > new.path",
+        "### diff.rb old.path new.path",
+        "### echo_path.rb > restored.path",
+        "### diff.rb old.path restored.path",
       ]
     end
   end
@@ -70,7 +70,7 @@ class CMDTEST_simple < Cmdtest::Testcase
   def test_try_to_run_non_existing_command_LINUX
     #
     return unless ! windows?
-        
+
     create_CMDTEST_foo [
       'cmd "non-existing" do',
       '    exit_nonzero',
diff --git a/t/bin/diff.rb b/t/bin/diff.rb
new file mode 100755
index 0000000..e616d8e
--- /dev/null
+++ b/t/bin/diff.rb
@@ -0,0 +1,10 @@
+#!/usr/bin/ruby
+
+a = File.read(ARGV[0], encoding: 'BINARY')
+b = File.read(ARGV[1], encoding: 'BINARY')
+if a == b
+    exit(0)
+else
+    puts "file differ: %s and %s" % [ARGV[0], ARGV[1]]
+    exit(1)
+end
diff --git a/t/bin/echo_path.rb b/t/bin/echo_path.rb
new file mode 100755
index 0000000..83ac7ff
--- /dev/null
+++ b/t/bin/echo_path.rb
@@ -0,0 +1,3 @@
+#!/usr/bin/ruby
+
+puts ENV['PATH']
diff --git a/t/bin/echo_pwd.rb b/t/bin/echo_pwd.rb
new file mode 100755
index 0000000..ec0ec0a
--- /dev/null
+++ b/t/bin/echo_pwd.rb
@@ -0,0 +1,3 @@
+#!/usr/bin/ruby
+
+puts "PWD=" + Dir.pwd
diff --git a/t/bin/find_files.rb b/t/bin/find_files.rb
new file mode 100755
index 0000000..1f02747
--- /dev/null
+++ b/t/bin/find_files.rb
@@ -0,0 +1,12 @@
+#!/usr/bin/ruby
+
+require "find"
+
+files = []
+Find.find('.') do |path|
+    if File.file?(path)
+        files << path
+    end
+end
+
+puts files.sort