scide 0.0.12 → 0.1.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.
Files changed (59) hide show
  1. data/.rspec +0 -1
  2. data/.ruby-version +1 -0
  3. data/.screenrc +8 -0
  4. data/.travis.yml +1 -1
  5. data/Gemfile +10 -11
  6. data/Gemfile.lock +46 -39
  7. data/LICENSE.txt +1 -1
  8. data/README.md +36 -144
  9. data/Rakefile +19 -26
  10. data/VERSION +1 -1
  11. data/bin/scide +1 -2
  12. data/lib/scide/auto.rb +33 -0
  13. data/lib/scide/list.rb +33 -0
  14. data/lib/scide/open.rb +57 -0
  15. data/lib/scide/program.rb +109 -0
  16. data/lib/scide/setup.rb +16 -0
  17. data/lib/scide.rb +25 -57
  18. data/scide.gemspec +51 -59
  19. data/spec/auto_spec.rb +72 -0
  20. data/spec/cli/list_spec.rb +81 -0
  21. data/spec/cli/open_spec.rb +131 -0
  22. data/spec/cli/setup_spec.rb +82 -0
  23. data/spec/helper.rb +12 -24
  24. data/spec/list_spec.rb +113 -0
  25. data/spec/open_spec.rb +228 -0
  26. data/spec/setup_spec.rb +106 -0
  27. data/spec/version_spec.rb +3 -5
  28. metadata +150 -72
  29. data/.document +0 -5
  30. data/.rvmrc +0 -41
  31. data/TODO.md +0 -21
  32. data/lib/scide/command.rb +0 -124
  33. data/lib/scide/commands/edit.rb +0 -37
  34. data/lib/scide/commands/run.rb +0 -25
  35. data/lib/scide/commands/show.rb +0 -29
  36. data/lib/scide/commands/tail.rb +0 -37
  37. data/lib/scide/config.rb +0 -105
  38. data/lib/scide/global.rb +0 -30
  39. data/lib/scide/opts.rb +0 -35
  40. data/lib/scide/overmind.rb +0 -70
  41. data/lib/scide/project.rb +0 -93
  42. data/lib/scide/screen.rb +0 -77
  43. data/lib/scide/window.rb +0 -88
  44. data/spec/command_spec.rb +0 -86
  45. data/spec/commands/edit_spec.rb +0 -19
  46. data/spec/commands/run_spec.rb +0 -9
  47. data/spec/commands/show_spec.rb +0 -15
  48. data/spec/commands/tail_spec.rb +0 -14
  49. data/spec/config_spec.rb +0 -113
  50. data/spec/global_spec.rb +0 -38
  51. data/spec/opts_spec.rb +0 -42
  52. data/spec/project_spec.rb +0 -171
  53. data/spec/results/config1.yml +0 -14
  54. data/spec/results/malformed_config.yml +0 -2
  55. data/spec/results/project1.screen +0 -10
  56. data/spec/results/screen1.screen +0 -14
  57. data/spec/scide_spec.rb +0 -38
  58. data/spec/screen_spec.rb +0 -90
  59. data/spec/window_spec.rb +0 -122
@@ -0,0 +1,109 @@
1
+ require 'commander'
2
+
3
+ module Scide
4
+
5
+ class Program < Commander::Runner
6
+
7
+ BACKTRACE_NOTICE = ' (use --trace to view backtrace)'
8
+
9
+ include Commander::UI
10
+ include Commander::UI::AskForClass
11
+
12
+ def initialize argv = ARGV
13
+ super argv
14
+
15
+ program :name, 'scide'
16
+ program :version, Scide::VERSION
17
+ program :description, 'GNU Screen IDE. Scide is a wrapper to launch screen with a .screenrc configuration file in the current directory or in project directories.'
18
+ program :help, 'H4LP', 'https://github.com/AlphaHydrae/scide/issues'
19
+
20
+ global_option '-p', '--projects PROJECTS_DIR', 'Use a custom projects directory (defaults to $SCIDE_PROJECTS or ~/src)'
21
+
22
+ command :open do |c|
23
+
24
+ c.syntax = 'scide open [PROJECT_NAME]'
25
+ c.description = 'Open the project in PROJECTS_DIR/PROJECT_NAME or the project in the current directory.'
26
+
27
+ c.option '-p', '--projects PROJECTS_DIR', 'Use a custom projects directory (defaults to $SCIDE_PROJECTS or ~/src)'
28
+ c.option '-a', '--auto', "Automatically use a default screen configuration if the current directory doesn't have a .screenrc file (defaults to $SCIDE_AUTO)"
29
+ c.option '-b', '--bin BIN', 'Path to screen binary (defaults to $SCIDE_BIN or searches in the PATH)'
30
+ c.option '-s', '--screen OPTIONS', 'Custom screen options (defaults to $SCIDE_SCREEN or -U)'
31
+ c.option '-n', '--noop', 'Show the command that would be run'
32
+
33
+ c.example 'scide', 'Open the project in the current directory (.screenrc required).'
34
+ c.example 'scide -p ~/src my-project', 'Open a named project from ~/src (config at ~/src/my-project/.screenrc).'
35
+ c.example 'scide my-project', 'Open a named project (from $SCIDE_PROJECTS or ~/src).'
36
+
37
+ c.action do |args,options|
38
+ to_trace_or_not_to_trace options.trace do
39
+ options = extract :projects, :auto, :bin, :screen, :noop, options
40
+ args << options unless options.empty?
41
+ result = Scide.open *args
42
+ puts result if options[:noop]
43
+ end
44
+ end
45
+ end
46
+
47
+ command :list do |c|
48
+
49
+ c.syntax = 'scide list'
50
+ c.description = 'List the projects with a .screenrc file in PROJECTS_DIR.'
51
+
52
+ c.option '-p', '--projects PROJECTS_DIR', 'Use a custom projects directory (defaults to $SCIDE_PROJECTS or ~/src)'
53
+
54
+ c.example 'scide list', 'List the projects with a .screenrc file in $SCIDE_PROJECTS or ~/src.'
55
+ c.example 'scide -p ~/Projects list', 'List the projects with a .screenrc file in ~/Projects.'
56
+
57
+ c.action do |args,options|
58
+ to_trace_or_not_to_trace options.trace do
59
+ projects = Scide.list extract(:projects, options)
60
+ puts projects.join("\n")
61
+ end
62
+ end
63
+ end
64
+
65
+ command :setup do |c|
66
+
67
+ c.syntax = 'scide setup [PROJECT_NAME]'
68
+ c.description = 'Create a default .screenrc file in PROJECTS_DIR/PROJECT_NAME or in the current directory.'
69
+
70
+ c.option '-p', '--projects PROJECTS_DIR', 'Use a custom projects directory (defaults to $SCIDE_PROJECTS or ~/src)'
71
+
72
+ c.example 'scide setup', 'Create a default .screenrc file in the current directory.'
73
+ c.example 'scide -p ~/src setup my-project', 'Create a default .screenrc file in ~/src/my-project.'
74
+ c.example 'scide setup my-project', 'Create a default .screenrc file in $SCIDE_PROJECTS/my-project or ~/src/my-project.'
75
+
76
+ c.action do |args,options|
77
+ to_trace_or_not_to_trace options.trace do
78
+ options = extract :projects, :noop, options
79
+ args << options unless options.empty?
80
+ result = Scide.setup *args
81
+ puts Paint[result, :green]
82
+ end
83
+ end
84
+ end
85
+
86
+ default_command :open
87
+ end
88
+
89
+ private
90
+
91
+ def to_trace_or_not_to_trace trace = false
92
+ begin
93
+ yield
94
+ rescue Scide::Error => e
95
+ if trace
96
+ raise e
97
+ else
98
+ warn Paint["#{e.message}#{BACKTRACE_NOTICE}", :red]
99
+ exit e.code
100
+ end
101
+ end
102
+ end
103
+
104
+ def extract *args
105
+ options = args.pop
106
+ args.inject({}){ |memo,k| memo[k] = options.__send__(k); memo }.reject{ |k,v| v.nil? }
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,16 @@
1
+
2
+ module Scide
3
+
4
+ def self.setup *args
5
+
6
+ options = args.last.kind_of?(Hash) ? args.pop : {}
7
+ dir = current_project_dir args, options
8
+
9
+ file = File.join dir, '.screenrc'
10
+ error "#{file} already exists" if File.exists? file
11
+
12
+ File.open(file, 'w'){ |f| f.write auto_config }
13
+
14
+ file
15
+ end
16
+ end
data/lib/scide.rb CHANGED
@@ -1,71 +1,39 @@
1
1
  require 'paint'
2
- require 'upoj-rb'
2
+ require 'which_works'
3
+ require 'shellwords'
3
4
 
4
- # Generator of GNU Screen configuration files.
5
5
  module Scide
6
+ VERSION = '0.1.0'
6
7
 
7
- # Current version.
8
- VERSION = File.open(File.join(File.dirname(__FILE__), '..', 'VERSION'), 'r').read
9
-
10
- # Exit status codes.
11
- EXIT = {
12
- :unexpected => 1,
13
- :invalid_argument => 2,
14
- :not_initialized => 3,
15
- :screen_not_found => 4,
16
- :config_not_found => 10,
17
- :config_not_readable => 11,
18
- :malformed_config => 12,
19
- :invalid_config => 13,
20
- :unknown_project => 14
21
- }
22
-
23
- # Prints a message on <tt>stderr</tt> and exits.
24
- # If <tt>condition</tt> is a key from {EXIT}, the corresponding value
25
- # will be used as the exit code. Otherwise, scide exits with
26
- # status 1.
27
- def self.fail condition, msg
28
- if @@exit_on_fail
29
- puts
30
- warn Paint[msg, :yellow]
31
- puts
32
- EXIT.key?(condition) ? exit(EXIT[condition]) : exit(1)
33
- else
34
- raise Scide::Error.new condition, msg
8
+ # TODO: add detailed error description for non-trace mode
9
+ class Error < StandardError
10
+ attr_reader :code
11
+
12
+ def initialize msg, code = 1
13
+ super msg
14
+ @code = code
35
15
  end
36
16
  end
37
17
 
38
- # By default, scide is meant to be used as a standalone script
39
- # and exits if an error occurs. If <tt>exit_on_fail</tt> is
40
- # false, a {Scide::Error} will be raised instead. Scide can then
41
- # be used by another script.
42
- def self.exit_on_fail= exit_on_fail
43
- @@exit_on_fail = exit_on_fail
44
- end
18
+ private
45
19
 
46
- # Indicates whether scide is configured to exit on failure.
47
- # See {Scide.exit_on_fail=}.
48
- def self.exit_on_fail
49
- @@exit_on_fail
20
+ def self.error msg, code = 1
21
+ raise Error.new msg, code
50
22
  end
51
23
 
52
- # Scide error. Can be raised if {exit_on_fail} is set to false.
53
- class Error < StandardError
54
-
55
- # A symbol indicating the error type. See {EXIT}.
56
- attr_reader :condition
57
-
58
- # Returns a new error.
59
- def initialize condition, msg
60
- super msg
61
- @condition = condition
62
- end
24
+ def self.projects_dir options = {}
25
+ options[:projects] || ENV['SCIDE_PROJECTS'] || File.expand_path('~/src')
63
26
  end
64
27
 
65
- private
66
-
67
- @@exit_on_fail = true
28
+ def self.current_project_dir args, options = {}
29
+ project_name = args.shift
30
+ error "Only one project name must be given, got #{args.unshift project_name}" if args.any?
31
+ dir = project_name ? File.join(projects_dir(options), project_name) : Dir.pwd
32
+ error %/Cannot use home directory/ if File.expand_path(dir) == File.expand_path('~')
33
+ error %/No such directory "#{dir}"/ unless File.directory? dir
34
+ dir
35
+ end
68
36
  end
69
37
 
70
- # load scide components
71
- %w( command config global opts overmind project screen window ).each{ |dep| require File.join(File.dirname(__FILE__), 'scide', dep) }
38
+ Dir[File.join File.dirname(__FILE__), File.basename(__FILE__, '.*'), '*.rb'].each{ |lib| require lib }
39
+
data/scide.gemspec CHANGED
@@ -5,12 +5,12 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "scide"
8
- s.version = "0.0.12"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["AlphaHydrae"]
12
- s.date = "2012-03-09"
13
- s.description = "Utility to generate GNU screen configuration files."
11
+ s.authors = ["Simon Oulevay (AlphaHydrae)"]
12
+ s.date = "2013-05-02"
13
+ s.description = "GNU screen wrapper to open projects with a .screenrc file."
14
14
  s.email = "hydrae.alpha@gmail.com"
15
15
  s.executables = ["scide"]
16
16
  s.extra_rdoc_files = [
@@ -18,94 +18,86 @@ Gem::Specification.new do |s|
18
18
  "README.md"
19
19
  ]
20
20
  s.files = [
21
- ".document",
22
21
  ".rspec",
23
- ".rvmrc",
22
+ ".ruby-version",
23
+ ".screenrc",
24
24
  ".travis.yml",
25
25
  "Gemfile",
26
26
  "Gemfile.lock",
27
27
  "LICENSE.txt",
28
28
  "README.md",
29
29
  "Rakefile",
30
- "TODO.md",
31
30
  "VERSION",
32
31
  "bin/scide",
33
32
  "lib/scide.rb",
34
- "lib/scide/command.rb",
35
- "lib/scide/commands/edit.rb",
36
- "lib/scide/commands/run.rb",
37
- "lib/scide/commands/show.rb",
38
- "lib/scide/commands/tail.rb",
39
- "lib/scide/config.rb",
40
- "lib/scide/global.rb",
41
- "lib/scide/opts.rb",
42
- "lib/scide/overmind.rb",
43
- "lib/scide/project.rb",
44
- "lib/scide/screen.rb",
45
- "lib/scide/window.rb",
33
+ "lib/scide/auto.rb",
34
+ "lib/scide/list.rb",
35
+ "lib/scide/open.rb",
36
+ "lib/scide/program.rb",
37
+ "lib/scide/setup.rb",
46
38
  "scide.gemspec",
47
- "spec/command_spec.rb",
48
- "spec/commands/edit_spec.rb",
49
- "spec/commands/run_spec.rb",
50
- "spec/commands/show_spec.rb",
51
- "spec/commands/tail_spec.rb",
52
- "spec/config_spec.rb",
53
- "spec/global_spec.rb",
39
+ "spec/auto_spec.rb",
40
+ "spec/cli/list_spec.rb",
41
+ "spec/cli/open_spec.rb",
42
+ "spec/cli/setup_spec.rb",
54
43
  "spec/helper.rb",
55
- "spec/opts_spec.rb",
56
- "spec/project_spec.rb",
57
- "spec/results/config1.yml",
58
- "spec/results/malformed_config.yml",
59
- "spec/results/project1.screen",
60
- "spec/results/screen1.screen",
61
- "spec/scide_spec.rb",
62
- "spec/screen_spec.rb",
63
- "spec/version_spec.rb",
64
- "spec/window_spec.rb"
44
+ "spec/list_spec.rb",
45
+ "spec/open_spec.rb",
46
+ "spec/setup_spec.rb",
47
+ "spec/version_spec.rb"
65
48
  ]
66
49
  s.homepage = "http://github.com/AlphaHydrae/scide"
67
50
  s.licenses = ["MIT"]
68
51
  s.require_paths = ["lib"]
69
- s.rubygems_version = "1.8.17"
70
- s.summary = "GNU Screen IDE."
52
+ s.rubygems_version = "1.8.25"
53
+ s.summary = "GNU Screen IDE"
71
54
 
72
55
  if s.respond_to? :specification_version then
73
56
  s.specification_version = 3
74
57
 
75
58
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
76
- s.add_runtime_dependency(%q<upoj-rb>, ["~> 0.0.4"])
59
+ s.add_runtime_dependency(%q<commander>, ["~> 4.1.3"])
60
+ s.add_runtime_dependency(%q<paint>, ["~> 0.8.6"])
77
61
  s.add_runtime_dependency(%q<which_works>, ["~> 0.1.0"])
78
- s.add_development_dependency(%q<rspec>, [">= 0"])
79
- s.add_development_dependency(%q<shoulda>, [">= 0"])
80
62
  s.add_development_dependency(%q<bundler>, [">= 0"])
81
- s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
63
+ s.add_development_dependency(%q<rake>, [">= 0"])
64
+ s.add_development_dependency(%q<rspec>, [">= 0"])
65
+ s.add_development_dependency(%q<fakefs>, [">= 0"])
66
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
67
+ s.add_development_dependency(%q<gemcutter>, [">= 0"])
68
+ s.add_development_dependency(%q<gem-release>, [">= 0"])
69
+ s.add_development_dependency(%q<rake-version>, [">= 0"])
82
70
  s.add_development_dependency(%q<simplecov>, [">= 0"])
83
- s.add_development_dependency(%q<yard>, [">= 0"])
84
- s.add_development_dependency(%q<rdiscount>, [">= 0"])
85
- s.add_development_dependency(%q<travis-lint>, ["~> 1.3.0"])
71
+ s.add_development_dependency(%q<travis-lint>, [">= 0"])
86
72
  else
87
- s.add_dependency(%q<upoj-rb>, ["~> 0.0.4"])
73
+ s.add_dependency(%q<commander>, ["~> 4.1.3"])
74
+ s.add_dependency(%q<paint>, ["~> 0.8.6"])
88
75
  s.add_dependency(%q<which_works>, ["~> 0.1.0"])
89
- s.add_dependency(%q<rspec>, [">= 0"])
90
- s.add_dependency(%q<shoulda>, [">= 0"])
91
76
  s.add_dependency(%q<bundler>, [">= 0"])
92
- s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
77
+ s.add_dependency(%q<rake>, [">= 0"])
78
+ s.add_dependency(%q<rspec>, [">= 0"])
79
+ s.add_dependency(%q<fakefs>, [">= 0"])
80
+ s.add_dependency(%q<jeweler>, [">= 0"])
81
+ s.add_dependency(%q<gemcutter>, [">= 0"])
82
+ s.add_dependency(%q<gem-release>, [">= 0"])
83
+ s.add_dependency(%q<rake-version>, [">= 0"])
93
84
  s.add_dependency(%q<simplecov>, [">= 0"])
94
- s.add_dependency(%q<yard>, [">= 0"])
95
- s.add_dependency(%q<rdiscount>, [">= 0"])
96
- s.add_dependency(%q<travis-lint>, ["~> 1.3.0"])
85
+ s.add_dependency(%q<travis-lint>, [">= 0"])
97
86
  end
98
87
  else
99
- s.add_dependency(%q<upoj-rb>, ["~> 0.0.4"])
88
+ s.add_dependency(%q<commander>, ["~> 4.1.3"])
89
+ s.add_dependency(%q<paint>, ["~> 0.8.6"])
100
90
  s.add_dependency(%q<which_works>, ["~> 0.1.0"])
101
- s.add_dependency(%q<rspec>, [">= 0"])
102
- s.add_dependency(%q<shoulda>, [">= 0"])
103
91
  s.add_dependency(%q<bundler>, [">= 0"])
104
- s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
92
+ s.add_dependency(%q<rake>, [">= 0"])
93
+ s.add_dependency(%q<rspec>, [">= 0"])
94
+ s.add_dependency(%q<fakefs>, [">= 0"])
95
+ s.add_dependency(%q<jeweler>, [">= 0"])
96
+ s.add_dependency(%q<gemcutter>, [">= 0"])
97
+ s.add_dependency(%q<gem-release>, [">= 0"])
98
+ s.add_dependency(%q<rake-version>, [">= 0"])
105
99
  s.add_dependency(%q<simplecov>, [">= 0"])
106
- s.add_dependency(%q<yard>, [">= 0"])
107
- s.add_dependency(%q<rdiscount>, [">= 0"])
108
- s.add_dependency(%q<travis-lint>, ["~> 1.3.0"])
100
+ s.add_dependency(%q<travis-lint>, [">= 0"])
109
101
  end
110
102
  end
111
103
 
data/spec/auto_spec.rb ADDED
@@ -0,0 +1,72 @@
1
+ require 'helper'
2
+ require 'fileutils'
3
+
4
+ describe Scide do
5
+ include FakeFS::SpecHelpers
6
+
7
+ before :each do
8
+ FileUtils.mkdir_p '/tmp'
9
+ end
10
+
11
+ context ".auto_config_file" do
12
+
13
+ before(:each){ Scide.stub auto_config: 'foo' }
14
+
15
+ it "should create a temporary configuration file" do
16
+ Scide.auto_config_file do |auto_file|
17
+ expect(auto_file).to be_a_kind_of(Tempfile)
18
+ end
19
+ end
20
+
21
+ it "should create a file containing the results of ::auto_config" do
22
+ Scide.auto_config_file do |auto_file|
23
+ expect(File.read(auto_file)).to eq('foo')
24
+ end
25
+ end
26
+ end
27
+
28
+ context ".auto_config" do
29
+
30
+ let(:expected_config){ %|screen -t editor 0\nstuff "\\${PROJECT_EDITOR-\\$EDITOR}\\012"\nscreen -t shell 1\nselect editor| }
31
+ subject{ Scide.auto_config }
32
+
33
+ it{ should eq(expected_config) }
34
+
35
+ context "with a .screenrc file in the home directory" do
36
+ let(:expected_config){ %|source $HOME/.screenrc\n\n#{super()}| }
37
+ before(:each){ setup '~' }
38
+ it{ should eq(expected_config) }
39
+ end
40
+ end
41
+
42
+ context ".auto?" do
43
+
44
+ let(:options){ {} }
45
+ let(:env_auto){ nil }
46
+ subject{ Scide.auto? options }
47
+ it{ should be_false }
48
+
49
+ before :each do
50
+ ENV['SCIDE_AUTO'] = env_auto if env_auto
51
+ end
52
+
53
+ context "with the auto option" do
54
+ let(:options){ { auto: true } }
55
+ it{ should be_true }
56
+ end
57
+
58
+ [ '1', 'y', 'yes', 't', 'true' ].each do |valid|
59
+ context "with $SCIDE_AUTO set to #{valid}" do
60
+ let(:env_auto){ valid }
61
+ it{ should be_true }
62
+ end
63
+ end
64
+ end
65
+
66
+ def setup dir, config = true
67
+ dir = File.expand_path dir
68
+ FileUtils.mkdir_p dir
69
+ FileUtils.touch File.join(dir, '.screenrc') if config
70
+ dir
71
+ end
72
+ end
@@ -0,0 +1,81 @@
1
+ require 'helper'
2
+
3
+ describe 'scide list' do
4
+
5
+ let(:argv){ [ 'list' ] }
6
+ let(:expected_options){ {} }
7
+
8
+ before :each do
9
+ Scide.stub :list
10
+ Scide.stub(:open).and_raise(StandardError.new('open should not be called'))
11
+ Scide.stub(:setup).and_raise(StandardError.new('setup should not be called'))
12
+ end
13
+
14
+ shared_examples_for "list" do
15
+
16
+ it "should output nothing if there are no projects" do
17
+ Scide.stub list: []
18
+ expect_success "\n"
19
+ end
20
+
21
+ it "should output the list of projects" do
22
+ Scide.stub list: [ '.', 'a', 'b', 'c' ]
23
+ expect_success ".\na\nb\nc\n"
24
+ end
25
+
26
+ context "if it fails" do
27
+
28
+ before :each do
29
+ Scide.stub(:list).and_raise(Scide::Error.new('fubar'))
30
+ end
31
+
32
+ it "should output the error to stderr" do
33
+ expect_failure "fubar#{Scide::Program::BACKTRACE_NOTICE}"
34
+ end
35
+
36
+ context "with the trace option" do
37
+
38
+ let(:argv){ super().unshift '--trace' }
39
+
40
+ it "should raise the error" do
41
+ expect_failure "fubar", true
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ it_behaves_like "list"
48
+
49
+ [ '-p', '--projects' ].each do |opt|
50
+ context "with the #{opt} option" do
51
+ let(:projects_dir){ '~/Projects' }
52
+ let(:expected_options){ { projects: projects_dir } }
53
+ let(:argv){ super() + [ opt, projects_dir ] }
54
+ it_behaves_like "list"
55
+ end
56
+ end
57
+
58
+ def expect_success output
59
+ Scide.should_receive(:list).with expected_options
60
+ program = Scide::Program.new argv
61
+ stdout, stderr = StringIO.new, StringIO.new
62
+ $stdout, $stderr = stdout, stderr
63
+ expect{ program.run! }.to_not raise_error
64
+ $stdout, $stderr = STDOUT, STDERR
65
+ expect(stdout.string).to eq(output)
66
+ expect(stderr.string).to be_empty
67
+ end
68
+
69
+ def expect_failure message, expect_raise = false, code = 1, &block
70
+ program = Scide::Program.new argv
71
+ if expect_raise
72
+ expect{ program.run! }.to raise_error(Scide::Error){ |e| expect(e.message).to eq(message) }
73
+ else
74
+ stderr = StringIO.new
75
+ $stderr = stderr
76
+ expect{ program.run! }.to raise_error(SystemExit){ |e| expect(e.status).to eq(code) }
77
+ $stderr = STDERR
78
+ expect(Paint.unpaint(stderr.string.strip)).to eq(message)
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,131 @@
1
+ require 'helper'
2
+
3
+ describe 'scide open' do
4
+
5
+ let(:argv){ [] }
6
+ let(:expected_args){ [] }
7
+ let(:expected_options){ {} }
8
+ let(:expected_output){ expected_options[:noop] ? fake_command : '' }
9
+ let(:fake_command){ 'foo' }
10
+
11
+ before :each do
12
+ Scide.stub :open
13
+ Scide.stub(:list).and_raise(StandardError.new('list should not be called'))
14
+ Scide.stub(:setup).and_raise(StandardError.new('setup should not be called'))
15
+ end
16
+
17
+ shared_examples_for "open" do
18
+
19
+ before :each do
20
+ Scide.stub open: fake_command
21
+ end
22
+
23
+ it "should open the current directory" do
24
+ expect_success
25
+ end
26
+
27
+ context "with a project name" do
28
+ let(:argv){ super().push 'a' }
29
+ let(:expected_args){ [ 'a' ] }
30
+
31
+ it "should open the project" do
32
+ expect_success
33
+ end
34
+ end
35
+
36
+ context "if it fails" do
37
+
38
+ before :each do
39
+ Scide.stub(:open).and_raise(Scide::Error.new('fubar'))
40
+ end
41
+
42
+ it "should output the error to stderr" do
43
+ expect_failure "fubar#{Scide::Program::BACKTRACE_NOTICE}"
44
+ end
45
+
46
+ context "with the trace option" do
47
+
48
+ let(:argv){ super().unshift '--trace' }
49
+
50
+ it "should raise the error" do
51
+ expect_failure "fubar", true
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ it_behaves_like "open"
58
+
59
+ [ '-p', '--projects' ].each do |opt|
60
+ context "with the #{opt} option" do
61
+ let(:projects_dir){ '~/Projects' }
62
+ let(:expected_options){ { projects: projects_dir } }
63
+ let(:argv){ [ opt, projects_dir ] }
64
+ it_behaves_like "open"
65
+ end
66
+ end
67
+
68
+ [ '-a', '--auto' ].each do |opt|
69
+ context "with the #{opt} option" do
70
+ let(:expected_options){ { auto: true } }
71
+ let(:argv){ [ opt ] }
72
+ it_behaves_like "open"
73
+ end
74
+ end
75
+
76
+ [ '-n', '--noop' ].each do |opt|
77
+ context "with the #{opt} option" do
78
+ let(:expected_options){ { noop: true } }
79
+ let(:argv){ [ opt ] }
80
+ it_behaves_like "open"
81
+ end
82
+ end
83
+
84
+ [ '-b', '--bin' ].each do |opt|
85
+ context "with the #{opt} option" do
86
+ let(:expected_options){ { bin: '/bin/custom-screen' } }
87
+ let(:argv){ [ opt, '/bin/custom-screen' ] }
88
+ it_behaves_like "open"
89
+ end
90
+ end
91
+
92
+ [ '-s', '--screen' ].each do |opt|
93
+ context "with the #{opt} option" do
94
+ let(:expected_options){ { screen: '-a' } }
95
+ let(:argv){ [ opt, "-a" ] }
96
+ it_behaves_like "open"
97
+ end
98
+ end
99
+
100
+ context "with all options" do
101
+ let(:projects_dir){ '~/Projects' }
102
+ let(:expected_options){ { projects: projects_dir, auto: true, bin: '/bin/custom-screen', screen: '-r -x', noop: true } }
103
+ let(:argv){ [ '--projects', projects_dir, '--auto', '--bin', '/bin/custom-screen', '--screen', '-r -x', '--noop' ] }
104
+ it_behaves_like "open"
105
+ end
106
+
107
+ def expect_success *args
108
+ args = args.empty? ? expected_args + (expected_options.empty? ? [] : [ expected_options ]) : args
109
+ Scide.should_receive(:open).with *args
110
+ program = Scide::Program.new argv
111
+ stdout, stderr = StringIO.new, StringIO.new
112
+ $stdout, $stderr = stdout, stderr
113
+ expect{ program.run! }.to_not raise_error
114
+ $stdout, $stderr = STDOUT, STDERR
115
+ expect(Paint.unpaint(stdout.string.chomp "\n")).to eq(expected_output)
116
+ expect(stderr.string).to be_empty
117
+ end
118
+
119
+ def expect_failure message, expect_raise = false, code = 1, &block
120
+ program = Scide::Program.new argv
121
+ if expect_raise
122
+ expect{ program.run! }.to raise_error(Scide::Error){ |e| expect(e.message).to eq(message) }
123
+ else
124
+ stderr = StringIO.new
125
+ $stderr = stderr
126
+ expect{ program.run! }.to raise_error(SystemExit){ |e| expect(e.status).to eq(code) }
127
+ expect(Paint.unpaint(stderr.string.strip)).to eq(message)
128
+ $stderr = STDERR
129
+ end
130
+ end
131
+ end