about summary refs log tree commit homepage
diff options
context:
space:
mode:
-rw-r--r--lib/dtas/player.rb4
-rw-r--r--lib/dtas/player/client_handler.rb11
-rw-r--r--test/test_player_integration.rb21
3 files changed, 36 insertions, 0 deletions
diff --git a/lib/dtas/player.rb b/lib/dtas/player.rb
index b26303c..e055fa5 100644
--- a/lib/dtas/player.rb
+++ b/lib/dtas/player.rb
@@ -180,6 +180,10 @@ class DTAS::Player
       io.emit("OK")
     when "source"
       source_handler(io, msg)
+    when "cd"
+      chdir_handler(io, msg)
+    when "pwd"
+      io.emit(Dir.pwd)
     end
   end
 
diff --git a/lib/dtas/player/client_handler.rb b/lib/dtas/player/client_handler.rb
index aabd1ab..b3c208d 100644
--- a/lib/dtas/player/client_handler.rb
+++ b/lib/dtas/player/client_handler.rb
@@ -449,4 +449,15 @@ module DTAS::Player::ClientHandler
       io.emit("ERR unknown source op")
     end
   end
+
+  def chdir_handler(io, msg)
+    msg.size == 1 or return io.emit("ERR usage: cd DIRNAME")
+    begin
+      Dir.chdir(msg[0])
+    rescue => e
+      return io.emit("ERR chdir: #{e.message}")
+    end
+    # echo(%W(cd msg[0])) # should we broadcast this?
+    io.emit("OK")
+  end
 end
diff --git a/test/test_player_integration.rb b/test/test_player_integration.rb
index f7b1306..4ce0752 100644
--- a/test/test_player_integration.rb
+++ b/test/test_player_integration.rb
@@ -196,4 +196,25 @@ class TestPlayerIntegration < Minitest::Unit::TestCase
     assert(system("cmp", dump.path, expect.path),
            "files don't match #{dump.path} != #{expect.path}")
   end
+
+  def test_cd_pwd
+    s = client_socket
+    pwd = Dir.pwd
+
+    s.preq("pwd")
+    assert_equal pwd, s.readpartial(6666)
+
+    s.preq("cd /")
+    assert_equal "OK", s.readpartial(6)
+
+    s.preq("pwd")
+    assert_equal "/", s.readpartial(6)
+
+    s.preq("cd /this-better-be-totally-non-existent-on-any-system-#{rand}")
+    err = s.readpartial(666)
+    assert_match(%r{\AERR }, err, err)
+
+    s.preq("pwd")
+    assert_equal "/", s.readpartial(6)
+  end
 end