scriptster 0.1.0 → 0.2.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/.travis.yml +15 -0
- data/CHANGELOG.md +6 -0
- data/README.md +44 -16
- data/gemrc +2 -0
- data/lib/scriptster.rb +28 -2
- data/lib/scriptster/configuration.rb +29 -6
- data/lib/scriptster/logger.rb +71 -12
- data/lib/scriptster/shellcmd.rb +23 -0
- data/lib/scriptster/version.rb +2 -1
- data/scriptster.gemspec +1 -0
- data/spec/scriptster_spec.rb +221 -0
- data/spec/utils.rb +26 -0
- metadata +25 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 29ca137ce4703b707c5f4f423e16a52ad0f20fc0
|
4
|
+
data.tar.gz: 1188b577e385809ff3408006ec80cacf3ef4f97a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 61d9c1c7b7713cee0d0f15b9734daa9b9bf171e613d4fcd4dd522104ddda9eb03efb3298aaea1d7e2e42782a2514ff48bfaa730e61e4d111a02937a8ae67a8c7
|
7
|
+
data.tar.gz: 2725686e52306e57082efe967a2ec5576c6cc2e72eb75cc189d985a303d26c8b4878f258abfb9ddb48f73ffeee10776b692f66affe6f40e2af9fa30ef6c826ab
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -1,39 +1,67 @@
|
|
1
1
|
# Scriptster
|
2
2
|
|
3
|
-
|
3
|
+
[](http://badge.fury.io/rb/scriptster)
|
4
|
+
[](http://inch-ci.org/github/pazdera/scriptster)
|
5
|
+
[](https://travis-ci.org/pazdera/scriptster)
|
4
6
|
|
5
|
-
|
7
|
+
Scriptster is a small library to help you writing scripts in Ruby. It
|
8
|
+
is only consists of two functions and it's especially useful for apps
|
9
|
+
that uses many external tools through the shell.
|
6
10
|
|
7
|
-
|
11
|
+
The two basic things this library focuses on are
|
12
|
+
* Running shell commands
|
13
|
+
* Providing nice logs/status messages about what happened
|
8
14
|
|
9
|
-
|
10
|
-
|
11
|
-
And then execute:
|
12
|
-
|
13
|
-
$ bundle
|
14
|
-
|
15
|
-
Or install it yourself as:
|
16
|
-
|
17
|
-
$ gem install scriptster
|
15
|
+
See the examples bellow.
|
18
16
|
|
19
17
|
## Usage
|
20
18
|
|
21
|
-
TODO: Write usage instructions here
|
22
|
-
|
23
19
|
```ruby
|
24
20
|
require 'scriptster'
|
21
|
+
```
|
25
22
|
|
23
|
+
It is not necessary to configure scriptster before using it, in case you're
|
24
|
+
fine with the default settings. But if not, the configure method offers
|
25
|
+
many options to change. For the full list of options, please refer to
|
26
|
+
the [docs](http://www.rubydoc.info/github/pazdera/scriptster/master/frames).
|
27
|
+
|
28
|
+
```ruby
|
26
29
|
Scriptster::configure do |conf|
|
27
|
-
conf
|
30
|
+
conf.name = "my-script"
|
31
|
+
conf.theme = :dark
|
28
32
|
end
|
33
|
+
```
|
34
|
+
|
35
|
+
The following snippet demonstrates exactly how would you use **scriptster**
|
36
|
+
in practice:
|
29
37
|
|
38
|
+
```ruby
|
30
39
|
Scriptster::log :info, "Starting ..."
|
31
40
|
|
32
|
-
Scriptster::
|
41
|
+
Scriptster::cmd.new "git branch",
|
33
42
|
:show_out = true,
|
34
43
|
:show_err = true,
|
35
44
|
```
|
36
45
|
|
46
|
+
The `log` method has one more argument available and the `cmd` method
|
47
|
+
has other options that you can use. Again, you will find more in the
|
48
|
+
[docs](http://www.rubydoc.info/github/pazdera/scriptster/master/frames).
|
49
|
+
|
50
|
+
## Installation
|
51
|
+
|
52
|
+
Add this line to your application's Gemfile:
|
53
|
+
|
54
|
+
gem 'scriptster'
|
55
|
+
|
56
|
+
And then execute:
|
57
|
+
|
58
|
+
$ bundle
|
59
|
+
|
60
|
+
Or install it yourself as:
|
61
|
+
|
62
|
+
$ gem install scriptster
|
63
|
+
|
64
|
+
|
37
65
|
## Contributing
|
38
66
|
|
39
67
|
1. Fork it ( https://github.com/[my-github-username]/scriptster/fork )
|
data/gemrc
ADDED
data/lib/scriptster.rb
CHANGED
@@ -25,23 +25,49 @@ require "scriptster/logger"
|
|
25
25
|
require "scriptster/shellcmd"
|
26
26
|
require "scriptster/configuration"
|
27
27
|
|
28
|
+
# The public interface of scriptster is simple. It consists of two
|
29
|
+
# functions, cmd and log. The module can be used directly or included
|
30
|
+
# in your class.
|
28
31
|
module Scriptster
|
32
|
+
# Pass a message to the logger.
|
33
|
+
#
|
34
|
+
# @see Logger.log
|
29
35
|
def self.log(*args)
|
30
36
|
Logger::log *args
|
31
37
|
end
|
32
38
|
|
39
|
+
# The same as {Scriptster.log}.
|
40
|
+
#
|
41
|
+
# @see .log
|
33
42
|
def log(*args)
|
34
|
-
|
43
|
+
self.log *args
|
35
44
|
end
|
36
45
|
|
46
|
+
# Execute a shell command
|
47
|
+
#
|
48
|
+
# @see ShellCmd
|
37
49
|
def self.cmd(*args)
|
38
50
|
ShellCmd.new *args
|
39
51
|
end
|
40
52
|
|
53
|
+
# The same as {Scriptster.cmd}.
|
54
|
+
#
|
55
|
+
# @see .cmd
|
41
56
|
def cmd(*args)
|
42
|
-
|
57
|
+
self.cmd *args
|
43
58
|
end
|
44
59
|
|
60
|
+
# Use this method to reconfigure the library.
|
61
|
+
#
|
62
|
+
# @example
|
63
|
+
# Scriptster::configure do |conf|
|
64
|
+
# conf.name = "my-script"
|
65
|
+
# conf.colours = :light
|
66
|
+
# conf.timestamps = false
|
67
|
+
# end
|
68
|
+
#
|
69
|
+
# @yield [c] An instance of the {Configuration} class.
|
70
|
+
# @see Configuration
|
45
71
|
def self.configure
|
46
72
|
c = Configuration.new
|
47
73
|
yield c
|
@@ -23,30 +23,52 @@
|
|
23
23
|
require "scriptster/logger"
|
24
24
|
require "scriptster/shellcmd"
|
25
25
|
|
26
|
+
|
26
27
|
module Scriptster
|
28
|
+
# The configuration obejct used in the {Scriptster.configure} method.
|
29
|
+
#
|
30
|
+
# @attr [String] name The name of the script to be displayed in the logs.
|
31
|
+
# @attr [Symbol] verbosity The minimum verbosity to of messages to be displayed.
|
32
|
+
# @attr [String, File, StringIO] file A log file that all messages will be written to.
|
33
|
+
# @attr [Boolean] timestamps Include timestamps in log messages.
|
34
|
+
# @attr [Symbol, Proc] colours Desired colour theme (either predefined or custom Proc).
|
35
|
+
# @see ColourThemes
|
36
|
+
# @attr [String] log_format Template for each line in the logs
|
27
37
|
class Configuration
|
28
|
-
attr_writer :name, :verbosity, :file, :
|
38
|
+
attr_writer :name, :verbosity, :file, :colours, :log_format, :theme
|
29
39
|
|
30
40
|
def initialize
|
31
41
|
@name = File.basename($0)
|
32
42
|
@verbosity = :verbose
|
33
43
|
@file = nil
|
34
44
|
@timestamps = true
|
35
|
-
@
|
45
|
+
@colours = :dark
|
46
|
+
@log_format = "%{timestamp} %{name} %{type} %{message}"
|
36
47
|
end
|
37
48
|
|
38
|
-
# Put the settings from this object in effect
|
49
|
+
# Put the settings from this object in effect.
|
50
|
+
#
|
51
|
+
# This function will distribute the configuration to the
|
52
|
+
# appropriate objects and modules.
|
39
53
|
def apply
|
40
54
|
Logger.set_name @name if @name
|
41
55
|
Logger.set_verbosity @verbosity if @verbosity
|
42
56
|
Logger.set_file @file if @file
|
43
|
-
Logger.
|
57
|
+
Logger.set_format @log_format if @log_format
|
44
58
|
|
45
|
-
|
59
|
+
if @colours.is_a? Proc
|
60
|
+
@colours.call
|
61
|
+
else
|
62
|
+
ColourThemes.send @colours.to_sym
|
63
|
+
end
|
46
64
|
end
|
47
65
|
end
|
48
66
|
|
49
|
-
|
67
|
+
# A collection of predefined colour settings.
|
68
|
+
#
|
69
|
+
# It's basically a just configuring the tco library.
|
70
|
+
module ColourThemes
|
71
|
+
# The colour theme for dark terminals.
|
50
72
|
def self.dark
|
51
73
|
Tco::configure do |conf|
|
52
74
|
conf.names["green"] = "#99ad6a"
|
@@ -114,6 +136,7 @@ module Scriptster
|
|
114
136
|
end
|
115
137
|
end
|
116
138
|
|
139
|
+
# The colour scheme for dark terminals.
|
117
140
|
def self.light
|
118
141
|
Tco::configure do |conf|
|
119
142
|
conf.names["green"] = "#99ad6a"
|
data/lib/scriptster/logger.rb
CHANGED
@@ -24,11 +24,18 @@
|
|
24
24
|
require "tco"
|
25
25
|
|
26
26
|
module Scriptster
|
27
|
+
# This module contains the logging function and related configuration.
|
27
28
|
module Logger
|
29
|
+
# The name of the script.
|
28
30
|
@@name = nil
|
31
|
+
|
32
|
+
# The IO object to log to.
|
29
33
|
@@file = nil
|
34
|
+
|
35
|
+
# Show timestamps flag.
|
30
36
|
@@timestamps = false
|
31
37
|
|
38
|
+
# Supported message types.
|
32
39
|
@@message_types = {
|
33
40
|
:info => "info",
|
34
41
|
:warn => "WARN",
|
@@ -36,7 +43,10 @@ module Scriptster
|
|
36
43
|
:debug => "dbg?"
|
37
44
|
}
|
38
45
|
|
46
|
+
# The default verobosity level.
|
39
47
|
@@verbosity = :verbose
|
48
|
+
|
49
|
+
# Supported verbosity levels.
|
40
50
|
@@verbosity_levels = {
|
41
51
|
:quiet => 0,
|
42
52
|
:essential => 1,
|
@@ -45,10 +55,18 @@ module Scriptster
|
|
45
55
|
:verbose => 4
|
46
56
|
}
|
47
57
|
|
58
|
+
@@format = "%{timestamp} %{name} %{type} %{message}"
|
59
|
+
|
60
|
+
# A setter for the script name.
|
61
|
+
#
|
62
|
+
# @param [String] name Desired script name.
|
48
63
|
def self.set_name(name)
|
49
64
|
@@name = name
|
50
65
|
end
|
51
66
|
|
67
|
+
# A setter for for logger verbosity.
|
68
|
+
#
|
69
|
+
# @param [Symbol] level Desired verbosity level.
|
52
70
|
def self.set_verbosity(level)
|
53
71
|
msg = "Message verbosity level not recognised (#{})."
|
54
72
|
raise msg unless @@verbosity_levels.has_key? level.to_sym
|
@@ -56,26 +74,60 @@ module Scriptster
|
|
56
74
|
@@verbosity = level.to_sym
|
57
75
|
end
|
58
76
|
|
77
|
+
# A setter for the log file.
|
78
|
+
#
|
79
|
+
# @param [String, StringIO, File] file A path or an IO object.
|
59
80
|
def self.set_file(file)
|
60
81
|
@@file.close if @@file
|
61
82
|
@@file = nil
|
62
|
-
|
83
|
+
|
84
|
+
case
|
85
|
+
when file.is_a?(String) then @@file = File.open file, "w"
|
86
|
+
when file.is_a?(File) then @@file = file
|
87
|
+
when file.is_a?(StringIO) then @@file = file
|
88
|
+
else
|
89
|
+
raise "Not a vailid file"
|
90
|
+
end
|
63
91
|
end
|
64
92
|
|
65
|
-
|
66
|
-
|
93
|
+
# Specify the format of each line in the logs.
|
94
|
+
#
|
95
|
+
# The template can reference the following keys:
|
96
|
+
# * timestamp
|
97
|
+
# * name
|
98
|
+
# * type
|
99
|
+
# * message
|
100
|
+
#
|
101
|
+
# @example
|
102
|
+
# Logger::set_format "%{timestamp} %{name} %{type} %{message}"
|
103
|
+
#
|
104
|
+
# @param [String] format The format template.
|
105
|
+
def self.set_format(format)
|
106
|
+
@@format = format
|
67
107
|
end
|
68
108
|
|
109
|
+
# Log a string.
|
110
|
+
#
|
111
|
+
# The message will be written to both stdout and the log file if configured.
|
112
|
+
#
|
113
|
+
# @param [Symbol] msg_type Type of the message.
|
114
|
+
# @param [String] msg The contents of the log message.
|
115
|
+
# @param [Symbol] verbosity Desired verbosity level of this message.
|
69
116
|
def self.log(msg_type, msg, verbosity=:informative)
|
117
|
+
# arguments sanity checks
|
118
|
+
unless @@message_types.include? msg_type
|
119
|
+
raise ArgumentError, "Unknown message type :#{msg_type}"
|
120
|
+
end
|
121
|
+
|
122
|
+
unless @@verbosity_levels.include?(verbosity) and verbosity != :quiet
|
123
|
+
raise ArgumentError, "You can't use the :#{verbosity.to_s} verbosity level"
|
124
|
+
end
|
125
|
+
|
70
126
|
if @@verbosity_levels[verbosity] <= @@verbosity_levels[@@verbosity]
|
71
|
-
|
72
|
-
Time.now.strftime("%Y-%m-%d %H:%M").style("timestamp") + " "
|
73
|
-
else
|
74
|
-
""
|
75
|
-
end
|
127
|
+
ts = Time.now.strftime("%Y-%m-%d %H:%M:%S").style("timestamp")
|
76
128
|
|
77
129
|
name = if @@name != nil && @@name.length > 0
|
78
|
-
@@name.style("name")
|
130
|
+
@@name.style("name")
|
79
131
|
else
|
80
132
|
""
|
81
133
|
end
|
@@ -83,9 +135,13 @@ module Scriptster
|
|
83
135
|
msg.chomp!
|
84
136
|
msg = Tco::parse msg, Tco::get_style("#{msg_type.to_s}-message")
|
85
137
|
|
86
|
-
line =
|
87
|
-
|
88
|
-
|
138
|
+
line = @@format % {
|
139
|
+
timestamp: ts,
|
140
|
+
name: name,
|
141
|
+
type: @@message_types[msg_type].style(msg_type.to_s),
|
142
|
+
message: msg
|
143
|
+
}
|
144
|
+
|
89
145
|
puts line
|
90
146
|
STDOUT.flush
|
91
147
|
|
@@ -96,6 +152,9 @@ module Scriptster
|
|
96
152
|
end
|
97
153
|
end
|
98
154
|
|
155
|
+
# Instance method wrapper for when the module is included.
|
156
|
+
#
|
157
|
+
# @see Logger.log
|
99
158
|
def log(msg_type, msg, verbosity=:informative)
|
100
159
|
Logger::log msg_type, msg, verbosity
|
101
160
|
end
|
data/lib/scriptster/shellcmd.rb
CHANGED
@@ -26,11 +26,31 @@ require "tco"
|
|
26
26
|
require "scriptster/logger"
|
27
27
|
|
28
28
|
module Scriptster
|
29
|
+
# Represent an executed shell command.
|
30
|
+
#
|
31
|
+
# The command will be executed in the constructor. It runs in the
|
32
|
+
# foreground, so your application will block until it's finished
|
33
|
+
# executing. The logs, however, will be printed real-time as the
|
34
|
+
# command prints its output.
|
35
|
+
#
|
36
|
+
# @attr [Process::status] status The exit status of the command.
|
37
|
+
# @attr [String] out The content of the STDOUT of the command.
|
38
|
+
# @attr [String] err The content of the STDERR of the command.
|
29
39
|
class ShellCmd
|
30
40
|
attr_reader :status, :out, :err
|
31
41
|
|
32
42
|
include Logger
|
33
43
|
|
44
|
+
# Initialise the object and run the command
|
45
|
+
#
|
46
|
+
# @param [String] cmd The command line to be run.
|
47
|
+
# @param [Hash] opts Various options of the command.
|
48
|
+
# @option opts [Boolean] :show_out Care about STDOUT flag.
|
49
|
+
# @option opts [Boolean] :show_err Care about STDERR flag.
|
50
|
+
# @option opts [Boolean] :raise Raise on error flag.
|
51
|
+
# @option opts [String] :tag Logger tag (defaults to the first
|
52
|
+
# word of the command line).
|
53
|
+
# @option opts [Integer, Array<Integer>] :expect Expected return values.
|
34
54
|
def initialize(cmd, opts={})
|
35
55
|
@out = ""
|
36
56
|
@err = ""
|
@@ -52,6 +72,9 @@ module Scriptster
|
|
52
72
|
end
|
53
73
|
|
54
74
|
private
|
75
|
+
# Execute the command and collect all the data from it.
|
76
|
+
#
|
77
|
+
# The function will blog until the command has finished.
|
55
78
|
def run
|
56
79
|
Open3.popen3(@cmd) do |stdin, stdout, stderr, wait_thr|
|
57
80
|
stdout_buffer=""
|
data/lib/scriptster/version.rb
CHANGED
data/scriptster.gemspec
CHANGED
@@ -0,0 +1,221 @@
|
|
1
|
+
# scriptster - A small library to make your scipts a bit nicer
|
2
|
+
# Copyright (c) 2014 Radek Pazdera
|
3
|
+
|
4
|
+
# MIT License
|
5
|
+
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
# of this software and associated documentation files (the "Software"), to deal
|
8
|
+
# in the Software without restriction, including without limitation the rights
|
9
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
# copies of the Software, and to permit persons to whom the Software is
|
11
|
+
# furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
# The above copyright notice and this permission notice shall be included in
|
14
|
+
# all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22
|
+
|
23
|
+
require 'stringio'
|
24
|
+
require 'scriptster'
|
25
|
+
require 'utils'
|
26
|
+
require 'tmpdir'
|
27
|
+
|
28
|
+
describe Scriptster do
|
29
|
+
describe "#log" do
|
30
|
+
before :all do
|
31
|
+
@out = StringIO.new
|
32
|
+
Scriptster::configure do |conf|
|
33
|
+
conf.file = @out
|
34
|
+
end
|
35
|
+
|
36
|
+
@stdout = StringIO.new
|
37
|
+
end
|
38
|
+
|
39
|
+
before :each do
|
40
|
+
@out.truncate 0
|
41
|
+
|
42
|
+
@stdout.truncate 0
|
43
|
+
@orig_stdout = $stdout
|
44
|
+
$stdout = @stdout
|
45
|
+
end
|
46
|
+
|
47
|
+
after :each do
|
48
|
+
$stdout = @orig_stdout
|
49
|
+
end
|
50
|
+
|
51
|
+
it "logs to info" do
|
52
|
+
Scriptster::log :info, "Hello there"
|
53
|
+
expect(@out.string.strip).to match /info Hello there/
|
54
|
+
end
|
55
|
+
|
56
|
+
it "logs to warning" do
|
57
|
+
Scriptster::log :warn, "Hello there"
|
58
|
+
expect(@out.string.strip).to match /WARN Hello there/
|
59
|
+
end
|
60
|
+
|
61
|
+
it "logs to error" do
|
62
|
+
Scriptster::log :err, "Hello there"
|
63
|
+
expect(@out.string.strip).to match /ERR\! Hello there/
|
64
|
+
end
|
65
|
+
|
66
|
+
it "logs to debug" do
|
67
|
+
Scriptster::log :debug, "Hello there"
|
68
|
+
expect(@out.string.strip).to match /dbg\? Hello there/
|
69
|
+
end
|
70
|
+
|
71
|
+
it "raises on bad message type" do
|
72
|
+
expect {
|
73
|
+
Scriptster::log(:abcd, "Hello there")
|
74
|
+
}.to raise_exception ArgumentError
|
75
|
+
end
|
76
|
+
|
77
|
+
it "raises on bad verbosity type" do
|
78
|
+
expect {
|
79
|
+
Scriptster::log(:info, "Hello there", :quiet)
|
80
|
+
}.to raise_exception ArgumentError
|
81
|
+
end
|
82
|
+
|
83
|
+
it "is quiet" do
|
84
|
+
Scriptster::configure do |conf|
|
85
|
+
conf.verbosity = :quiet
|
86
|
+
end
|
87
|
+
|
88
|
+
Scriptster::log :info, "Hello there"
|
89
|
+
expect(@out.string.strip).not_to match /info Hello there/
|
90
|
+
end
|
91
|
+
|
92
|
+
it "default verbosity is less than :essential" do
|
93
|
+
Scriptster::configure do |conf|
|
94
|
+
conf.verbosity = :essential
|
95
|
+
end
|
96
|
+
|
97
|
+
Scriptster::log :info, "Hello there"
|
98
|
+
expect(@out.string.strip).not_to match /info Hello there/
|
99
|
+
end
|
100
|
+
|
101
|
+
it "default verbosity is :informative" do
|
102
|
+
Scriptster::configure do |conf|
|
103
|
+
conf.verbosity = :informative
|
104
|
+
end
|
105
|
+
|
106
|
+
Scriptster::log :info, "Hello there"
|
107
|
+
expect(@out.string.strip).to match /info Hello there/
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
describe "#cmd" do
|
112
|
+
before :all do
|
113
|
+
# Set up a temporary test directory
|
114
|
+
@dir = Dir.mktmpdir
|
115
|
+
|
116
|
+
File.open("#{@dir}/file.txt", "w") do |f|
|
117
|
+
f.puts "Multi-line"
|
118
|
+
f.puts "File contents"
|
119
|
+
end
|
120
|
+
|
121
|
+
# Set up the dummy stdout/stderr
|
122
|
+
@out = StringIO.new
|
123
|
+
@err = StringIO.new
|
124
|
+
end
|
125
|
+
|
126
|
+
after :all do
|
127
|
+
FileUtils.remove_entry_secure @dir
|
128
|
+
end
|
129
|
+
|
130
|
+
before :each do
|
131
|
+
@out.truncate 0
|
132
|
+
@orig_stdout = $stdout
|
133
|
+
$stdout = @out
|
134
|
+
|
135
|
+
@err.truncate 0
|
136
|
+
@orig_stderr = $stderr
|
137
|
+
$stderr = @err
|
138
|
+
end
|
139
|
+
|
140
|
+
after :each do
|
141
|
+
$stdout = @orig_stdout
|
142
|
+
|
143
|
+
$stderr = @orig_stderr
|
144
|
+
end
|
145
|
+
|
146
|
+
it "shows output" do
|
147
|
+
Scriptster::cmd "cat #{@dir}/file.txt", :show_out => true
|
148
|
+
|
149
|
+
line1, line2 = remove_colours(@out.string.strip).split "\n"
|
150
|
+
expect(line1).to match "Multi-line"
|
151
|
+
expect(line2).to match "File contents"
|
152
|
+
end
|
153
|
+
|
154
|
+
it "hides output" do
|
155
|
+
Scriptster::cmd "cat #{@dir}/file.txt", :show_out => false
|
156
|
+
expect(@out.string).to eq ""
|
157
|
+
end
|
158
|
+
|
159
|
+
it "captures output" do
|
160
|
+
cat = Scriptster::cmd "cat #{@dir}/file.txt", :show_out => false
|
161
|
+
expect(cat.out).to eq "Multi-line\nFile contents\n"
|
162
|
+
end
|
163
|
+
|
164
|
+
it "shows error" do
|
165
|
+
Scriptster::cmd "cat #{@dir}/nonexistent.txt",
|
166
|
+
:show_err => true,
|
167
|
+
:raise => false
|
168
|
+
|
169
|
+
expect(@out.string.strip).to match "No such file or directory"
|
170
|
+
end
|
171
|
+
|
172
|
+
it "hides error" do
|
173
|
+
Scriptster::cmd "cat #{@dir}/nonexistent.txt",
|
174
|
+
:show_err => false,
|
175
|
+
:raise => false
|
176
|
+
|
177
|
+
expect(@out.string.strip).to match ""
|
178
|
+
end
|
179
|
+
|
180
|
+
it "captures error" do
|
181
|
+
cat = Scriptster::cmd "cat #{@dir}/nonexistent.txt",
|
182
|
+
:show_err => true,
|
183
|
+
:raise => false
|
184
|
+
|
185
|
+
expect(cat.err.strip).to match "No such file or directory"
|
186
|
+
end
|
187
|
+
|
188
|
+
it "collects status (no error)" do
|
189
|
+
cat = Scriptster::cmd "cat #{@dir}/file.txt",
|
190
|
+
:show_err => true,
|
191
|
+
:raise => false
|
192
|
+
|
193
|
+
expect(cat.status.exitstatus).to eq 0
|
194
|
+
end
|
195
|
+
|
196
|
+
it "collects status (fail)" do
|
197
|
+
cat = Scriptster::cmd "cat #{@dir}/nonexistent.txt",
|
198
|
+
:show_err => true,
|
199
|
+
:raise => false
|
200
|
+
|
201
|
+
expect(cat.status.exitstatus).to eq 1
|
202
|
+
end
|
203
|
+
|
204
|
+
it "does raise" do
|
205
|
+
expect {
|
206
|
+
Scriptster::cmd "cat #{@dir}/nonexistent.txt"
|
207
|
+
}.to raise_exception RuntimeError
|
208
|
+
end
|
209
|
+
|
210
|
+
it "expects correct return value" do
|
211
|
+
cat = Scriptster::cmd "cat #{@dir}/nonexistent.txt", :expect => 1
|
212
|
+
expect(cat.status.exitstatus).to eq 1
|
213
|
+
end
|
214
|
+
|
215
|
+
it "expects incorrect return value" do
|
216
|
+
expect {
|
217
|
+
Scriptster::cmd "cat #{@dir}/nonexistent.txt", :expect => 10
|
218
|
+
}.to raise_exception RuntimeError
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
data/spec/utils.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# scriptster - A small library to make your scipts a bit nicer
|
2
|
+
# Copyright (c) 2014 Radek Pazdera
|
3
|
+
|
4
|
+
# MIT License
|
5
|
+
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
# of this software and associated documentation files (the "Software"), to deal
|
8
|
+
# in the Software without restriction, including without limitation the rights
|
9
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
# copies of the Software, and to permit persons to whom the Software is
|
11
|
+
# furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
# The above copyright notice and this permission notice shall be included in
|
14
|
+
# all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22
|
+
|
23
|
+
|
24
|
+
def remove_colours(string)
|
25
|
+
string.gsub(/\033\[[0-9]+(;[0-9]+){0,2}m/, "")
|
26
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scriptster
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Radek Pazdera
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: tco
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '10.1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.1'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.1'
|
55
69
|
description: A simple library for making your scipts a bit nicer.
|
56
70
|
email:
|
57
71
|
- radek@pazdera.co.uk
|
@@ -60,16 +74,21 @@ extensions: []
|
|
60
74
|
extra_rdoc_files: []
|
61
75
|
files:
|
62
76
|
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- CHANGELOG.md
|
63
79
|
- Gemfile
|
64
80
|
- LICENSE.txt
|
65
81
|
- README.md
|
66
82
|
- Rakefile
|
83
|
+
- gemrc
|
67
84
|
- lib/scriptster.rb
|
68
85
|
- lib/scriptster/configuration.rb
|
69
86
|
- lib/scriptster/logger.rb
|
70
87
|
- lib/scriptster/shellcmd.rb
|
71
88
|
- lib/scriptster/version.rb
|
72
89
|
- scriptster.gemspec
|
90
|
+
- spec/scriptster_spec.rb
|
91
|
+
- spec/utils.rb
|
73
92
|
homepage: https://github.com/pazdera/scriptster
|
74
93
|
licenses:
|
75
94
|
- MIT
|
@@ -94,4 +113,7 @@ rubygems_version: 2.2.2
|
|
94
113
|
signing_key:
|
95
114
|
specification_version: 4
|
96
115
|
summary: Making your Ruby scipts hip.
|
97
|
-
test_files:
|
116
|
+
test_files:
|
117
|
+
- spec/scriptster_spec.rb
|
118
|
+
- spec/utils.rb
|
119
|
+
has_rdoc:
|