test-cmd.rb 0.11.1 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - data/README.md +2 -44
 - data/lib/test/cmd.rb +10 -26
 - data/test/{test_cmd_test.rb → cmd_test.rb} +2 -8
 - data/test-cmd.rb.gemspec +1 -1
 - metadata +3 -3
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA256:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: d794fcd3192700e47d0964074f19732cd9ebc6881804d267cdb182fd31c19f4a
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 139a5dda07f237f60918421a1ceefddb75f31807aaa9bb394c1f5e8680001a86
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: ad22764743a6f3e5d7a15457c8fe495f3f03598ca145cdb31754e69ce24a8a7c59bc76d74eb6022b2231b1ec311439e70da3bbfdc8c240cdcabb9f7c595d8fbf
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 9cb6ed663a9c544a8a6d1b83781251a36eddf114d821f26d2f79c705d8945ea7d69215d77e627d8b44e8b82db203029bc4d28fa0eb161f541291259a66ea76fc
         
     | 
    
        data/README.md
    CHANGED
    
    | 
         @@ -8,8 +8,8 @@ a command. 
     | 
|
| 
       8 
8 
     | 
    
         
             
            ### Callbacks
         
     | 
| 
       9 
9 
     | 
    
         | 
| 
       10 
10 
     | 
    
         
             
            The success and failure callbacks provide hooks for when
         
     | 
| 
       11 
     | 
    
         
            -
            a command exits successfully or unsuccessfully. The  
     | 
| 
       12 
     | 
    
         
            -
             
     | 
| 
      
 11 
     | 
    
         
            +
            a command exits successfully or unsuccessfully. The callbacks
         
     | 
| 
      
 12 
     | 
    
         
            +
            are passed an instance of
         
     | 
| 
       13 
13 
     | 
    
         
             
            [Test::Cmd](https://0x1eef.github.io/x/test-cmd.rb/Test/Cmd.html):
         
     | 
| 
       14 
14 
     | 
    
         | 
| 
       15 
15 
     | 
    
         
             
            ``` ruby
         
     | 
| 
         @@ -55,46 +55,6 @@ class CmdTest < Test::Unit::TestCase 
     | 
|
| 
       55 
55 
     | 
    
         
             
            end
         
     | 
| 
       56 
56 
     | 
    
         
             
            ```
         
     | 
| 
       57 
57 
     | 
    
         | 
| 
       58 
     | 
    
         
            -
            ### IO#sync
         
     | 
| 
       59 
     | 
    
         
            -
             
     | 
| 
       60 
     | 
    
         
            -
            Sometimes it can be neccessary to bypass Ruby's internal buffer and flush
         
     | 
| 
       61 
     | 
    
         
            -
            output to the operating system immediately, otherwise there can be unexpected
         
     | 
| 
       62 
     | 
    
         
            -
            results. Consider the following example, where the output will be
         
     | 
| 
       63 
     | 
    
         
            -
            `bar\nfoo\n` rather than `foo\nbar\n`:
         
     | 
| 
       64 
     | 
    
         
            -
             
     | 
| 
       65 
     | 
    
         
            -
            ``` ruby
         
     | 
| 
       66 
     | 
    
         
            -
            ##
         
     | 
| 
       67 
     | 
    
         
            -
            # test.rb
         
     | 
| 
       68 
     | 
    
         
            -
            fork do
         
     | 
| 
       69 
     | 
    
         
            -
              sleep(1)
         
     | 
| 
       70 
     | 
    
         
            -
              puts "bar"
         
     | 
| 
       71 
     | 
    
         
            -
            end
         
     | 
| 
       72 
     | 
    
         
            -
            puts "foo"
         
     | 
| 
       73 
     | 
    
         
            -
            Process.wait
         
     | 
| 
       74 
     | 
    
         
            -
             
     | 
| 
       75 
     | 
    
         
            -
            ##
         
     | 
| 
       76 
     | 
    
         
            -
            # cmd.rb
         
     | 
| 
       77 
     | 
    
         
            -
            p cmd("ruby", "test.rb").stdout # => "bar\nfoo\n"
         
     | 
| 
       78 
     | 
    
         
            -
            ```
         
     | 
| 
       79 
     | 
    
         
            -
             
     | 
| 
       80 
     | 
    
         
            -
            And with output flushed to the operating system immediately:
         
     | 
| 
       81 
     | 
    
         
            -
             
     | 
| 
       82 
     | 
    
         
            -
            ``` ruby
         
     | 
| 
       83 
     | 
    
         
            -
            ##
         
     | 
| 
       84 
     | 
    
         
            -
            # test.rb
         
     | 
| 
       85 
     | 
    
         
            -
            $stdout.sync = true
         
     | 
| 
       86 
     | 
    
         
            -
            fork do
         
     | 
| 
       87 
     | 
    
         
            -
              sleep(1)
         
     | 
| 
       88 
     | 
    
         
            -
              puts "bar"
         
     | 
| 
       89 
     | 
    
         
            -
            end
         
     | 
| 
       90 
     | 
    
         
            -
            puts "foo"
         
     | 
| 
       91 
     | 
    
         
            -
            Process.wait
         
     | 
| 
       92 
     | 
    
         
            -
             
     | 
| 
       93 
     | 
    
         
            -
            ##
         
     | 
| 
       94 
     | 
    
         
            -
            # cmd.rb
         
     | 
| 
       95 
     | 
    
         
            -
            p cmd("ruby", "test.rb").stdout # => "foo\nbar\n"
         
     | 
| 
       96 
     | 
    
         
            -
            ```
         
     | 
| 
       97 
     | 
    
         
            -
             
     | 
| 
       98 
58 
     | 
    
         
             
            ## Documentation
         
     | 
| 
       99 
59 
     | 
    
         | 
| 
       100 
60 
     | 
    
         
             
            A complete API reference is available at
         
     | 
| 
         @@ -102,8 +62,6 @@ A complete API reference is available at 
     | 
|
| 
       102 
62 
     | 
    
         | 
| 
       103 
63 
     | 
    
         
             
            ## Install
         
     | 
| 
       104 
64 
     | 
    
         | 
| 
       105 
     | 
    
         
            -
            **Rubygems.org**
         
     | 
| 
       106 
     | 
    
         
            -
             
     | 
| 
       107 
65 
     | 
    
         
             
            test-cmd.rb can be installed via rubygems.org:
         
     | 
| 
       108 
66 
     | 
    
         | 
| 
       109 
67 
     | 
    
         
             
                gem install test-cmd.rb
         
     | 
    
        data/lib/test/cmd.rb
    CHANGED
    
    | 
         @@ -1,13 +1,12 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
             
     | 
| 
       2 
     | 
    
         
            -
             
     | 
| 
      
 1 
     | 
    
         
            +
            unless defined?(Test)
         
     | 
| 
      
 2 
     | 
    
         
            +
              module Test
         
     | 
| 
      
 3 
     | 
    
         
            +
              end
         
     | 
| 
      
 4 
     | 
    
         
            +
            end
         
     | 
| 
       3 
5 
     | 
    
         | 
| 
       4 
6 
     | 
    
         
             
            ##
         
     | 
| 
       5 
7 
     | 
    
         
             
            # test-cmd.rb provides an object oriented interface
         
     | 
| 
       6 
8 
     | 
    
         
             
            # for spawning a command.
         
     | 
| 
       7 
9 
     | 
    
         
             
            class Test::Cmd
         
     | 
| 
       8 
     | 
    
         
            -
              require "tempfile"
         
     | 
| 
       9 
     | 
    
         
            -
              require "securerandom"
         
     | 
| 
       10 
     | 
    
         
            -
             
     | 
| 
       11 
10 
     | 
    
         
             
              ##
         
     | 
| 
       12 
11 
     | 
    
         
             
              # @param [String] cmd
         
     | 
| 
       13 
12 
     | 
    
         
             
              #  A command to spawn
         
     | 
| 
         @@ -37,11 +36,13 @@ class Test::Cmd 
     | 
|
| 
       37 
36 
     | 
    
         | 
| 
       38 
37 
     | 
    
         
             
                tap do
         
     | 
| 
       39 
38 
     | 
    
         
             
                  @spawned = true
         
     | 
| 
       40 
     | 
    
         
            -
                  @out_io,  
     | 
| 
       41 
     | 
    
         
            -
                   
     | 
| 
      
 39 
     | 
    
         
            +
                  @out_io, out = IO.pipe
         
     | 
| 
      
 40 
     | 
    
         
            +
                  @err_io, err = IO.pipe
         
     | 
| 
      
 41 
     | 
    
         
            +
                  Process.spawn(@cmd, *@argv, {out:, err:})
         
     | 
| 
       42 
42 
     | 
    
         
             
                  Process.wait
         
     | 
| 
       43 
43 
     | 
    
         
             
                  @status = $?
         
     | 
| 
       44 
44 
     | 
    
         
             
                ensure
         
     | 
| 
      
 45 
     | 
    
         
            +
                  [out, err].each(&:close)
         
     | 
| 
       45 
46 
     | 
    
         
             
                  [stdout, stderr]
         
     | 
| 
       46 
47 
     | 
    
         
             
                end
         
     | 
| 
       47 
48 
     | 
    
         
             
              end
         
     | 
| 
         @@ -52,7 +53,7 @@ class Test::Cmd 
     | 
|
| 
       52 
53 
     | 
    
         
             
              def stdout
         
     | 
| 
       53 
54 
     | 
    
         
             
                @stdout ||= begin
         
     | 
| 
       54 
55 
     | 
    
         
             
                  spawn
         
     | 
| 
       55 
     | 
    
         
            -
                  out_io. 
     | 
| 
      
 56 
     | 
    
         
            +
                  out_io.read.tap { out_io.close }
         
     | 
| 
       56 
57 
     | 
    
         
             
                rescue IOError
         
     | 
| 
       57 
58 
     | 
    
         
             
                  @stdout
         
     | 
| 
       58 
59 
     | 
    
         
             
                end
         
     | 
| 
         @@ -64,7 +65,7 @@ class Test::Cmd 
     | 
|
| 
       64 
65 
     | 
    
         
             
              def stderr
         
     | 
| 
       65 
66 
     | 
    
         
             
                @stderr ||= begin
         
     | 
| 
       66 
67 
     | 
    
         
             
                  spawn
         
     | 
| 
       67 
     | 
    
         
            -
                  err_io. 
     | 
| 
      
 68 
     | 
    
         
            +
                  err_io.read.tap { err_io.close }
         
     | 
| 
       68 
69 
     | 
    
         
             
                rescue IOError
         
     | 
| 
       69 
70 
     | 
    
         
             
                  @stderr
         
     | 
| 
       70 
71 
     | 
    
         
             
                end
         
     | 
| 
         @@ -141,23 +142,6 @@ class Test::Cmd 
     | 
|
| 
       141 
142 
     | 
    
         
             
              private
         
     | 
| 
       142 
143 
     | 
    
         | 
| 
       143 
144 
     | 
    
         
             
              attr_reader :out_io, :err_io
         
     | 
| 
       144 
     | 
    
         
            -
             
     | 
| 
       145 
     | 
    
         
            -
              def spawn_io
         
     | 
| 
       146 
     | 
    
         
            -
                [
         
     | 
| 
       147 
     | 
    
         
            -
                  [".testcmd.stdout.#{ns}.", SecureRandom.alphanumeric(3)],
         
     | 
| 
       148 
     | 
    
         
            -
                  [".testcmd.stderr.#{ns}.", SecureRandom.alphanumeric(3)]
         
     | 
| 
       149 
     | 
    
         
            -
                ].map do
         
     | 
| 
       150 
     | 
    
         
            -
                  Tempfile.new(_1).tap do |file|
         
     | 
| 
       151 
     | 
    
         
            -
                    File.chmod(0, file.path)
         
     | 
| 
       152 
     | 
    
         
            -
                  ensure
         
     | 
| 
       153 
     | 
    
         
            -
                    file.unlink
         
     | 
| 
       154 
     | 
    
         
            -
                  end
         
     | 
| 
       155 
     | 
    
         
            -
                end
         
     | 
| 
       156 
     | 
    
         
            -
              end
         
     | 
| 
       157 
     | 
    
         
            -
             
     | 
| 
       158 
     | 
    
         
            -
              def ns
         
     | 
| 
       159 
     | 
    
         
            -
                [Process.pid, object_id].join(".")
         
     | 
| 
       160 
     | 
    
         
            -
              end
         
     | 
| 
       161 
145 
     | 
    
         
             
            end
         
     | 
| 
       162 
146 
     | 
    
         | 
| 
       163 
147 
     | 
    
         
             
            module Kernel
         
     | 
| 
         @@ -3,6 +3,7 @@ require_relative "setup" 
     | 
|
| 
       3 
3 
     | 
    
         
             
            class Test::Cmd
         
     | 
| 
       4 
4 
     | 
    
         
             
              class Test < Test::Unit::TestCase
         
     | 
| 
       5 
5 
     | 
    
         
             
                private
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
       6 
7 
     | 
    
         
             
                def ruby(str)
         
     | 
| 
       7 
8 
     | 
    
         
             
                  cmd "ruby", "-e", str
         
     | 
| 
       8 
9 
     | 
    
         
             
                end
         
     | 
| 
         @@ -57,7 +58,7 @@ class Test::Cmd 
     | 
|
| 
       57 
58 
     | 
    
         
             
                  end
         
     | 
| 
       58 
59 
     | 
    
         
             
                  puts "foo"
         
     | 
| 
       59 
60 
     | 
    
         
             
                  Process.wait
         
     | 
| 
       60 
     | 
    
         
            -
             
     | 
| 
      
 61 
     | 
    
         
            +
                  CODE
         
     | 
| 
       61 
62 
     | 
    
         
             
                  assert_equal "foo\nbar\n", ruby(code).stdout
         
     | 
| 
       62 
63 
     | 
    
         
             
                end
         
     | 
| 
       63 
64 
     | 
    
         
             
              end
         
     | 
| 
         @@ -93,13 +94,6 @@ class Test::Cmd 
     | 
|
| 
       93 
94 
     | 
    
         
             
                  end
         
     | 
| 
       94 
95 
     | 
    
         
             
                end
         
     | 
| 
       95 
96 
     | 
    
         | 
| 
       96 
     | 
    
         
            -
                def test_io_unlink_after_spawn
         
     | 
| 
       97 
     | 
    
         
            -
                  %i[out_io err_io].each do |io|
         
     | 
| 
       98 
     | 
    
         
            -
                    path = spawned_command.send(io).__getobj__.path
         
     | 
| 
       99 
     | 
    
         
            -
                    assert_equal false, File.exist?(path)
         
     | 
| 
       100 
     | 
    
         
            -
                  end
         
     | 
| 
       101 
     | 
    
         
            -
                end
         
     | 
| 
       102 
     | 
    
         
            -
             
     | 
| 
       103 
97 
     | 
    
         
             
                private
         
     | 
| 
       104 
98 
     | 
    
         | 
| 
       105 
99 
     | 
    
         
             
                def spawned_command
         
     | 
    
        data/test-cmd.rb.gemspec
    CHANGED
    
    | 
         @@ -5,7 +5,7 @@ Gem::Specification.new do |gem| 
     | 
|
| 
       5 
5 
     | 
    
         
             
              gem.authors = ["0x1eef"]
         
     | 
| 
       6 
6 
     | 
    
         
             
              gem.email = ["0x1eef@protonmail.com"]
         
     | 
| 
       7 
7 
     | 
    
         
             
              gem.homepage = "https://github.com/0x1eef/test-cmd.rb#readme"
         
     | 
| 
       8 
     | 
    
         
            -
              gem.version = "0. 
     | 
| 
      
 8 
     | 
    
         
            +
              gem.version = "0.12.0"
         
     | 
| 
       9 
9 
     | 
    
         
             
              gem.required_ruby_version = ">= 3.0"
         
     | 
| 
       10 
10 
     | 
    
         
             
              gem.licenses = ["0BSD"]
         
     | 
| 
       11 
11 
     | 
    
         
             
              gem.files = `git ls-files`.split($/)
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,14 +1,14 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: test-cmd.rb
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.12.0
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - '0x1eef'
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire:
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date: 2024- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2024-06-06 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: test-unit
         
     | 
| 
         @@ -99,8 +99,8 @@ files: 
     | 
|
| 
       99 
99 
     | 
    
         
             
            - lib/test-cmd.rb
         
     | 
| 
       100 
100 
     | 
    
         
             
            - lib/test/cmd.rb
         
     | 
| 
       101 
101 
     | 
    
         
             
            - test-cmd.rb.gemspec
         
     | 
| 
      
 102 
     | 
    
         
            +
            - test/cmd_test.rb
         
     | 
| 
       102 
103 
     | 
    
         
             
            - test/setup.rb
         
     | 
| 
       103 
     | 
    
         
            -
            - test/test_cmd_test.rb
         
     | 
| 
       104 
104 
     | 
    
         
             
            homepage: https://github.com/0x1eef/test-cmd.rb#readme
         
     | 
| 
       105 
105 
     | 
    
         
             
            licenses:
         
     | 
| 
       106 
106 
     | 
    
         
             
            - 0BSD
         
     |