Commits

Taku Miyakawa  committed e88fb24

Removes COMMAND module because it is messy (closing issue #485)

  • Participants
  • Parent commits 3acfd0b

Comments (0)

Files changed (8)

File src/main/kink/COMMAND.kn

-#!/usr/bin/env kink
-# vim: et sw=4 sts=4
-
-# Copyright (c) 2013 Miyakawa Taku
-# 
-# Permission is hereby granted, free of charge, to any person obtaining a copy
-# of this software and associated documentation files (the "Software"), to deal
-# in the Software without restriction, including without limitation the rights
-# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-# copies of the Software, and to permit persons to whom the Software is
-# furnished to do so, subject to the following conditions:
-# 
-# The above copyright notice and this permission notice shall be included in
-# all copies or substantial portions of the Software.
-# 
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-# THE SOFTWARE.
-
-use('USING.using')
-use('java.lang.Thread')
-use('java.lang.ProcessBuilder')
-use('java.lang.ProcessBuilder$Redirect')
-use('java.nio.charset.Charset')
-use('java.lang.String')
-
-:command = { (* :Args)
-    :Process_builder = ProcessBuilder.new(Args)
-    Process_builder.inheritIO
-    _command_from_pb(Process_builder)
-}
-
-:_command_from_pb = { (:Process_builder)
-    Command_proto.child('Process_builder' Process_builder)
-}
-
-:Command_trait = [
-    'dir' { > :Command (:Dir)
-        Command.Process_builder.directory(path(Dir.toString))
-    }
-    'join' { > :Command ()
-        :Process = Command.Process_builder.start
-        Command._spawn_in(Process)
-        Command._spawn_out(Process)
-        Command._spawn_err(Process)
-        Command._join_in
-        Command._join_out
-        Command._join_err
-        :Status = Process.waitFor
-        Command.:join = const_fun(Status)
-        Status
-    }
-    'text' { > :Command (:Coding = Charset.defaultCharset)
-        :Text = ()
-        Command.handle_out { (:Stream)
-            :Text.solid = Stream.text(Coding)
-        }
-        Command.join
-        Text
-    }
-    'bytes' { > :Command ()
-        :Bytes = ()
-        Command.handle_out { (:Stream) :Bytes.solid = Stream.bytes }
-        Command.join
-        Bytes
-    }
-    'give_text' { > :Command (:Text)
-        Command.handle_in { (:Stream) Stream.print(Text) }
-    }
-    'give_bytes' { > :Command (:Bytes)
-        Command.handle_in { (:Stream) Stream.write(Bytes) }
-    }
-    'merge_error' { > :Command ()
-        Command.Process_builder.redirectErrorStream(true)
-    }
-    'handle_in' { > :Command (:_block)
-        Command.Process_builder.redirectInput(Redirect.PIPE)
-        Command.:_spawn_in = { (:Process)
-            :Thread = Thread.new {
-                using(Process.getOutputStream $_block)
-            }
-            Command.:_join_in = { Thread.join }
-            Thread.start
-        }
-    }
-    'handle_out' { > :Command (:_block)
-        Command.Process_builder.redirectOutput(Redirect.PIPE)
-        Command.:_spawn_out = { (:Process)
-            :Thread = Thread.new {
-                using(Process.getInputStream $_block)
-            }
-            Command.:_join_out = { Thread.join }
-            Thread.start
-        }
-    }
-    'handle_error' { > :Command (:_block)
-        Command.Process_builder.redirectError(Redirect.PIPE)
-        Command.:_spawn_err = { (:Process)
-            :Thread = Thread.new {
-                using(Process.getErrorStream $_block)
-            }
-            Command.:_join_err = { Thread.join }
-            Thread.start
-        }
-    }
-    '_spawn_in' {}
-    '_spawn_out' {}
-    '_spawn_err' {}
-    '_join_in' {}
-    '_join_out' {}
-    '_join_err' {}
-    'command??' { > :Command
-        Command.:Process_builder.any?
-    }
-]
-
-:Command_proto = value(*** Command_trait)

File src/test/bintest/bin_command_test.kn

-#!/usr/bin/env kink
-# vim: et sw=4 sts=4
-
-# Copyright (c) 2013 Miyakawa Taku
-# 
-# Permission is hereby granted, free of charge, to any person obtaining a copy
-# of this software and associated documentation files (the "Software"), to deal
-# in the Software without restriction, including without limitation the rights
-# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-# copies of the Software, and to permit persons to whom the Software is
-# furnished to do so, subject to the following conditions:
-# 
-# The above copyright notice and this permission notice shall be included in
-# all copies or substantial portions of the Software.
-# 
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-# THE SOFTWARE.
-
-use('COMMAND.command')
-use('JAVA.byte')
-use('JAVA.null')
-use('TEST.test')
-use('java.lang.System')
-
-# Get exit status of the command
-test {
-    :Status = command('bin/kink' '-e' '42').join
-    Status.check(0)
-}
-
-test {
-    :Command = command('bin/kink' '-e' '10 / 0')
-    Command.handle_error {}
-    Command.join.check(1)
-}
-
-# Get the stdout as text/bytes
-test {
-    :Command = command('bin/kink' '-e' "print('voxxx')")
-    Command.text.check('voxxx')
-    Command.join.check(0)
-}
-
-test {
-    :Command = command('bin/kink' '-e' "use('JAVA') print(JAVA.byte.array(0x30 0x42).decode('UTF-16BE') 'UTF-8')")
-    Command.text('UTF-8').check("\u3042")
-    Command.join.check(0)
-}
-
-test {
-    :Command = command('bin/kink' '-e' "use('JAVA') stdout.write(JAVA.byte.array(10 20 30))")
-    Command.bytes.check(byte.array(10 20 30))
-    Command.join.check(0)
-}
-
-# Give the stdin
-test {
-    :Command = command('bin/kink' '-e' "print(stdin.text.gsub('x' 'X'))")
-    Command.give_text('voxxx')
-    Command.text.check('voXXX')
-    Command.join.check(0)
-}
-
-test {
-    :Command = command('bin/kink' '-e' 'print(stdin.bytes.list.sum)')
-    Command.give_bytes(byte.array(10 20 30))
-    Command.text.check('60')
-    Command.join.check(0)
-}
-
-# Merge stderr to stdout
-test {
-    :Command = command('bin/kink' '-e' "stderr.print('voxxx')")
-    Command.merge_error
-    Command.text.check('voxxx')
-    Command.join.check(0)
-}
-
-# Put to the stdin and get from the stdout and stderr
-test {
-    :Command = command('bin/kink' '-e' "print('voxxx')")
-    :Out = null
-    Command.handle_out { (:Stream)
-        :Out.solid = Stream.text
-    }
-    :Status = Command.join
-    [Out Status].check(['voxxx' 0])
-}
-
-test {
-    :Command = command('bin/kink' '-e' "stderr.print('voxxx')")
-    :Err = null
-    Command.handle_error { (:Stream)
-        :Err.solid = Stream.text
-    }
-    :Status = Command.join
-    [Err Status].check(['voxxx' 0])
-}
-
-test {
-    :Command = command('bin/kink' '-e' ":T = stdin.line print('out:' + T) stderr.print('err:' + T)")
-    Command.handle_in { (:Stream)
-        Stream.print('voxxx')
-    }
-    :Out = null
-    Command.handle_out { (:Stream)
-        :Out.solid = Stream.text
-    }
-    :Err = null
-    Command.handle_error { (:Stream)
-        :Err.solid = Stream.text
-    }
-    :Status = Command.join
-    [Out Err Status].check(['out:voxxx' 'err:voxxx' 0])
-}
-
-# Gives java options
-test {
-    :Command = command(
-        'bin/kink'
-        '-J-Dfoobar=FOO BAR'
-        '-e' "print(class('java.lang.System').getProperty('foobar'))"
-    )
-    Command.text.check('FOO BAR')
-}
-
-# Executes under a work directory
-test {
-    :Command = command(System.getProperty('bin.file') '-e' "stdout.write(path('expected.utf8').bytes)")
-    Command.dir('src/test/bintest')
-    Command.bytes.check(path('src/test/bintest/expected.utf8').bytes)
-}

File src/test/bintest/file_mode_test.kn

 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 # THE SOFTWARE.
 
+use('USING.using')
 use('TEST.test')
-use('COMMAND.command')
 use('java.lang.ClassLoader')
+use('java.lang.ProcessBuilder')
+
+:command_status_and_bytes = { (* :Command_tokens)
+    :Kink_process_builder = ProcessBuilder.new(*** Command_tokens)
+    Kink_process_builder.redirectInput(ProcessBuilder.Redirect.INHERIT)
+    Kink_process_builder.redirectError(ProcessBuilder.Redirect.INHERIT)
+    :Kink_process = Kink_process_builder.start
+    :Bytes = using(Kink_process.getInputStream) { (:Stdout)
+        Stdout.bytes
+    }
+    :Status = Kink_process.waitFor
+    [Status Bytes]
+}
 
 # Returns an expected text
-:expected = { ()
-    ClassLoader.getSystemClassLoader.getResourceAsStream('expected.utf8').text('utf-8')
+:expected_bytes = { ()
+    ClassLoader.getSystemClassLoader.getResourceAsStream('expected.utf8').bytes
 }
 
 # Test euc input
 test {
-    :Command = command('bin/kink' 'src/test/stub/kink/bintest/euc.kn')
-    Command.bytes.check(expected.bytes('UTF-8'))
-    Command.join.check(0)
+    command_status_and_bytes(
+        'bin/kink' 'src/test/stub/kink/bintest/euc.kn'
+    )
+    .check([0 expected_bytes])
 }
 
 # Test sjis input
 test {
-    :Command = command('bin/kink' 'src/test/stub/kink/bintest/sjis.kn')
-    Command.bytes.check(expected.bytes('UTF-8'))
-    Command.join.check(0)
+    command_status_and_bytes(
+        'bin/kink' 'src/test/stub/kink/bintest/sjis.kn'
+    )
+    .check([0 expected_bytes])
 }
 
 # Test default input
 test {
-    :Command = command('bin/kink' '-J-Dfile.encoding=US-ASCII' 'src/test/stub/kink/bintest/default.kn')
-    Command.bytes.check(expected.bytes('UTF-8'))
-    Command.join.check(0)
+    command_status_and_bytes(
+        'bin/kink' '-J-Dfile.encoding=US-ASCII' 'src/test/stub/kink/bintest/default.kn'
+    )
+    .check([0 expected_bytes])
 }

File src/test/bintest/interact_mode_test.kn

 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 # THE SOFTWARE.
 
+use('java.lang.Thread')
+use('java.lang.ProcessBuilder')
+use('java.lang.System')
+use('USING.using')
 use('TEST.test')
-use('COMMAND.command')
-use('java.lang.System')
 
 # Execute in the interactive mode
 test {
-    :Command = command('bin/kink')
-    Command.handle_in {
-        \0.print_line('10 + 20')
-        \0.print_line('10 + 20 +')
-        \0.print_line('30')
+    :Kink_process_builder = ProcessBuilder.new('bin/kink')
+    Kink_process_builder.redirectError(ProcessBuilder.Redirect.INHERIT)
+    :Kink_process = Kink_process_builder.start
+    Thread.new {
+        using(Kink_process.getOutputStream) { (:Stdin)
+            Stdin.print_line('10 + 20')
+            Stdin.print_line('10 + 20 +')
+            Stdin.print_line('30')
+        }
+    } .start
+    :Stdout_text = using(Kink_process.getInputStream) { (:Stdout)
+        Stdout.text
     }
-    :Text = Command.text
-    Text.check([
+    :Exit_status = Kink_process.waitFor
+    Exit_status.check(0)
+    Stdout_text.check([
         "kink001:01> => 30" + System.lineSeparator
         "kink002:01> kink002:02\\ => 60" + System.lineSeparator
         "kink003:01> " + System.lineSeparator

File src/test/bintest/line_separator_test.kn

 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 # THE SOFTWARE.
 
-use('COMMAND.command')
+use('USING.using')
+use('java.lang.ProcessBuilder')
 use('TEST.test')
 
 test {
-    :Command = command('bin/kink' "-J-Dline.separator=\t" '-e' "print_line('voxxx') print_line('twift')")
-    Command.text.check("voxxx\ttwift\t")
+    :Kink_process_builder = ProcessBuilder.new('bin/kink' "-J-Dline.separator=\t" '-e' "print_line('voxxx') print_line('twift')")
+    Kink_process_builder.redirectInput(ProcessBuilder.Redirect.INHERIT)
+    Kink_process_builder.redirectError(ProcessBuilder.Redirect.INHERIT)
+    :Kink_process = Kink_process_builder.start
+    :Text = using(Kink_process.getInputStream) { (:Stdout)
+        Stdout.text
+    }
+    Text.check("voxxx\ttwift\t")
 }

File src/test/bintest/stack_overflow_test.kn

 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 # THE SOFTWARE.
 
-use('COMMAND.command')
+use('java.lang.ProcessBuilder')
 use('TEST.test')
 
-# Simple exec
-test {
-    :Command = command('bin/kink' '-e' '{ :foo.do { foo 1 } } .try')
-    Command.join.check(0)
+test('StackOverflowError is caught by try function') {
+    :Kink_process_builder = ProcessBuilder.new('bin/kink' '-e' '{ :foo.do { foo 1 } } .try')
+    Kink_process_builder.redirectInput(ProcessBuilder.Redirect.INHERIT)
+    Kink_process_builder.redirectOutput(ProcessBuilder.Redirect.INHERIT)
+    Kink_process_builder.redirectError(ProcessBuilder.Redirect.INHERIT)
+    :Kink_process = Kink_process_builder.start
+    :Exit_status = Kink_process.waitFor
+    Exit_status.check(0)
 }

File src/test/bintest/stdin_mode_test.kn

 # THE SOFTWARE.
 
 use('TEST.test')
-use('COMMAND.command')
+use('USING.using')
+use('java.lang.Thread')
+use('java.lang.ProcessBuilder')
 use('java.lang.ClassLoader')
 
+# Gives bytes as stdin, and takes an exit status and bytes from Kink process
+:execute = { (:Command_tokens :Stdin_bytes)
+    :Kink_process_builder = ProcessBuilder.new(*** Command_tokens)
+    Kink_process_builder.redirectError(ProcessBuilder.Redirect.INHERIT)
+    :Kink_process = Kink_process_builder.start
+    Thread.new {
+        using(Kink_process.getOutputStream) { (:Stdin)
+            Stdin.write_bytes(Stdin_bytes)
+        }
+    } .start
+    :Stdout_bytes = using(Kink_process.getInputStream) { (:Stdout)
+        Stdout.bytes
+    }
+    :Exit_status = Kink_process.waitFor
+    [Exit_status Stdout_bytes]
+}
+
 # Returns an expected text
 :expected = { ()
-    ClassLoader.getSystemClassLoader.getResourceAsStream('expected.utf8').text('utf-8')
+    ClassLoader.getSystemClassLoader.getResourceAsStream('expected.utf8').bytes
 }
 
 # Test euc input
 test {
     :Stdin = path('src/test/stub/kink/bintest/euc.kn').bytes
-    :Command = command('bin/kink' '-')
-    Command.give_bytes(Stdin)
-    Command.bytes.check(expected.bytes('UTF-8'))
-    Command.join.check(0)
+    execute(['bin/kink' '-'] Stdin).check([0 expected])
 }
 
 # Test sjis input
 test {
     :Stdin = path('src/test/stub/kink/bintest/sjis.kn').bytes
-    :Command = command('bin/kink' '-')
-    Command.give_bytes(Stdin)
-    Command.bytes.check(expected.bytes('UTF-8'))
-    Command.join.check(0)
+    execute(['bin/kink' '-'] Stdin).check([0 expected])
 }
 
 # Test default input
 test {
     :Stdin = path('src/test/stub/kink/bintest/default.kn').bytes
-    :Command = command('bin/kink' '-J-Dfile.encoding=US-ASCII' '-')
-    Command.give_bytes(Stdin)
-    Command.bytes.check(expected.bytes('UTF-8'))
-    Command.join.check(0)
+    execute(['bin/kink' '-J-Dfile.encoding=US-ASCII' '-'] Stdin).check([0 expected])
 }

File src/test/kink/COMMAND_test.kn

-#!/usr/bin/env kink
-# vim: et sw=4 sts=4
-
-# Copyright (c) 2013 Miyakawa Taku
-# 
-# Permission is hereby granted, free of charge, to any person obtaining a copy
-# of this software and associated documentation files (the "Software"), to deal
-# in the Software without restriction, including without limitation the rights
-# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-# copies of the Software, and to permit persons to whom the Software is
-# furnished to do so, subject to the following conditions:
-# 
-# The above copyright notice and this permission notice shall be included in
-# all copies or substantial portions of the Software.
-# 
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-# THE SOFTWARE.
-
-use('COMMAND')
-use('TEST.test')
-use('java.lang.System')
-
-# Test COMMAND.command??
-test {
-    :Command = COMMAND.command(System.getProperty('bin.file') '-e' "stdout.write(path('expected.utf8').bytes)")
-    Command.command??.check(true)
-}
-
-test {
-    :Command = COMMAND.command(System.getProperty('bin.file') '-e' "stdout.write(path('expected.utf8').bytes)")
-    Command.parent.command??.check(false)
-}