yop 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f999a4178d91f2c183491af37837a814129fbc96
4
- data.tar.gz: b5e0ecf6508e6f5635401878aee77745e28bc236
3
+ metadata.gz: ce8d5a9ab1ad1beba6340296cd5e98942ac659bf
4
+ data.tar.gz: 3e33e2d2ac37d54986dfc75e605828dd232d3bb4
5
5
  SHA512:
6
- metadata.gz: 4b77f2388b91267826ad6bfe1fa3b6c5a3446be04ed41b417f49e470c224ea9322ccf654a0efda9896d9cc29d8716d4d2e20d8bb844ada5168a6e73ece27cb3a
7
- data.tar.gz: afa79f8dd233a9b50177e999e3b54acb37f101cc91b22d3ab1a1ab55086deea802ee8130e8c5e3683f5f5315045631d755bce97ecb41e04bc5ec8d900d8cad53
6
+ metadata.gz: e6f44612f40053437ba7fae152cb5619f86e4ad71e8596af49ee0fa81a484a599568746788b84ff35394c0258a948b262592c7de061c180e1f4592f52faa5ab7
7
+ data.tar.gz: 4a6d1b1e27767dec20b48b162a8a31c57c730d160d388b0c6ada55d2de2d836b072b4aca3fbe799a44ea6a25f3cde25c35c1744128ed0e62f1895dccc0f74332
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
Binary file
data/lib/yop/config.rb CHANGED
@@ -28,6 +28,13 @@ module Yop
28
28
  save_config
29
29
  end
30
30
 
31
+ # Force-reload the config. This shouldn't be needed unless you know what
32
+ # you're doing (e.g. testing the module).
33
+ # @return [Nil]
34
+ def reload!
35
+ read_config
36
+ end
37
+
31
38
  private
32
39
 
33
40
  def read_config
@@ -1,4 +1,16 @@
1
1
  # -*- coding: UTF-8 -*-
2
2
 
3
- # This exception is raised when get_template fails to find a template
4
- class NonExistentTemplate < Exception; end
3
+ # Base exception for Yop-related ones
4
+ class YopException < Exception; end
5
+
6
+ # Raised when get_template fails to find a template
7
+ class NonExistentTemplate < YopException; end
8
+
9
+ # Raised when a template variable is undefined
10
+ class UndefinedTemplateVariable < YopException; end
11
+
12
+ # Raised when a template variable has a wrong value
13
+ class BadTemplateVariableValue < YopException; end
14
+
15
+ # Raised when a template contains an unsupported file type
16
+ class UnsupportedFileType < YopException; end
data/lib/yop/home.rb CHANGED
@@ -3,13 +3,19 @@
3
3
  require "fileutils"
4
4
 
5
5
  module Yop
6
+ # The base path used for all files
7
+ @basepath = "~/.yop"
8
+
6
9
  class << self
10
+ # let applications change the base path
11
+ attr_accessor :basepath
12
+
7
13
  # Return the local Yop directory location. If an argument is given, it
8
14
  # assumes it's a Yop subcomponent and returns its location.
9
15
  # @param subcomponent [String] the subcomponent to look for
10
16
  # @return [String] the path to the local Yop directory or its subcomponent
11
17
  def home(subcomponent = "")
12
- File.expand_path("~/.yop/#{subcomponent}")
18
+ File.expand_path("#{@basepath}/#{subcomponent}")
13
19
  end
14
20
 
15
21
  # Initialize Yop's local directory
data/lib/yop/templates.rb CHANGED
@@ -5,6 +5,7 @@ require "fileutils"
5
5
  require_relative "home"
6
6
  require_relative "config"
7
7
  require_relative "exceptions"
8
+ require_relative "ui"
8
9
 
9
10
  module Yop
10
11
  class << self
@@ -28,14 +29,24 @@ module Yop
28
29
  class Template
29
30
  include FileUtils
30
31
 
32
+ # The UI used to get unknown variables
33
+ # @param value [Yop::UI]
34
+ # @return [Yop::UI]
35
+ attr_writer :ui
36
+
31
37
  # Create a new template from a base directory
32
38
  # @param base_directory [String] a path to an existing directory which will
33
- # be used as a source when this template
34
- # will be applied
35
- # @param vars [Hash]
36
- def initialize(base_directory, vars = {})
39
+ # be used as a source when this template will be applied
40
+ # @param vars [Hash] variables to use in this template. If a variable is
41
+ # found in the template but isn't in the hash it'll be retrieved
42
+ # from the template's ui (default is Yop::TerminalUI).
43
+ # @param config [Hash] additional configuration options
44
+ def initialize(base_directory, vars = {}, config = {})
37
45
  @base = base_directory
38
46
  @vars = vars
47
+ @config = config
48
+
49
+ compile_var_pattern!
39
50
  end
40
51
 
41
52
  # Apply the template on a directory. It creates it if it doesn't exist,
@@ -54,8 +65,11 @@ module Yop
54
65
  next if skip? path
55
66
 
56
67
  source = "#{@base}/#{path}"
68
+ path = replace_vars_in_path path
57
69
 
58
- if File.directory? source
70
+ if File.symlink? source
71
+ copy_entry source, path
72
+ elsif File.directory? source
59
73
  mkdir_p path
60
74
  elsif File.file? source
61
75
  File.open(path, "w") do |f|
@@ -63,34 +77,90 @@ module Yop
63
77
  f.write(content)
64
78
  end
65
79
  else
66
- puts "Warning: unsupported file: #{source}"
67
- next
80
+ fail UnsupportedFileType, source
68
81
  end
69
82
  mirror_perms source, path
70
83
  end
71
84
  end
72
85
  end
73
86
 
87
+ # Shortcut to add a template variable
88
+ # @param name [Any] the variable's name
89
+ # @param value [Any] the variable's value
90
+ # @return [Any] the provided value
91
+ def []=(name, value)
92
+ @vars[name.to_s] = value
93
+ end
94
+
74
95
  private
75
96
 
97
+ # @param path [String]
76
98
  def ignore_extension?(path)
77
- %w[.swp .swo .pyc .class].any? { |e| path.end_with?(e) }
99
+ %w[swp swo pyc class].any? { |e| path.end_with?(".#{e}") }
78
100
  end
79
101
 
102
+ # @param path [String]
80
103
  def skip?(path)
81
- return true if ignore_extension? path
82
-
83
- [/\.git/, /.~$/, /__pycache__/].any? { |reg| path =~ reg }
104
+ ignore_extension?(path) ||
105
+ [/\.git/, /.~$/, /__pycache__/].any? { |reg| path =~ reg }
84
106
  end
85
107
 
108
+ # Replace vars in a file
109
+ # @param source [String] the file path
86
110
  def replace_vars(source)
87
- # TODO
88
- File.read(source)
111
+ replace_vars_in_string File.read(source)
112
+ end
113
+
114
+ # Replace vars in a string
115
+ # @param text [String]
116
+ def replace_vars_in_string(text)
117
+ text.gsub(@var_pattern) do
118
+ name = Regexp.last_match[1]
119
+ @vars[name] || @vars[name.to_sym] || get_var(name)
120
+ end
89
121
  end
90
122
 
123
+ # Same as +replace_vars_in_string+ but might be modified in the future to
124
+ # perform some specific path checks.
125
+ # @param source [String]
126
+ # @return [String]
127
+ alias_method :replace_vars_in_path, :replace_vars_in_string
128
+
129
+ # @param name [String]
130
+ def get_var(name)
131
+ @vars[name] = (@ui ||= Yop::TerminalUI.new).get_var(name)
132
+ end
133
+
134
+ # Initialize @var_pattern
135
+ def compile_var_pattern!
136
+ opening = Regexp.escape(@config[:before_var] || "{(")
137
+ closing = Regexp.escape(@config[:after_var] || ")}")
138
+
139
+ @var_pattern = /#{opening}([_A-Z][_A-Z0-9]*)#{closing}/
140
+ end
141
+
142
+ # Try to mirror the permissions from a file to another
143
+ # @param source [String]
144
+ # @param target [String]
145
+ # @return [Boolean] +true+ if the permissions were successfully mirrored
91
146
  def mirror_perms(source, target)
147
+ return mirror_perms_symlink(source, target) if File.symlink? source
92
148
  mode = File.new(source).stat.mode
93
149
  File.chmod(mode, target)
150
+ true
151
+ end
152
+
153
+ # Try to mirror the permissions from a symlink to another. Use
154
+ # +mirror_perms+ if you're not sure if +source+ is a symlink.
155
+ # @param source [String]
156
+ # @param target [String]
157
+ # @return [Boolean] +true+ if the permissions were successfully mirrored
158
+ def mirror_perms_symlink(source, target)
159
+ mode = File.new(source).lstat.mode
160
+ File.lchmod(mode, target)
161
+ true
162
+ rescue NotImplementedError
163
+ false
94
164
  end
95
165
  end
96
166
  end
data/lib/yop/ui.rb ADDED
@@ -0,0 +1,27 @@
1
+ # -*- coding: UTF-8 -*-
2
+
3
+ require_relative "exceptions"
4
+
5
+ module Yop
6
+ # A base UI class
7
+ class UI
8
+ # get a variable's value. It'll fail with +UndefinedTemplateVariable+ if
9
+ # the variable can't be found.
10
+ # @param name [String] the variable's name
11
+ # @return [Any]
12
+ def get_var(name)
13
+ fail UndefinedTemplateVariable, name
14
+ end
15
+ end
16
+
17
+ # A terminal UI
18
+ class TerminalUI < UI
19
+ # Get a variable's value from the terminal
20
+ def get_var(name)
21
+ print "#{name} = "
22
+ $stdin.readline.chomp
23
+ rescue EOFError
24
+ super
25
+ end
26
+ end
27
+ end
data/lib/yop/version.rb CHANGED
@@ -3,6 +3,6 @@
3
3
  module Yop
4
4
  # @return [String] the current gem's version
5
5
  def self.version
6
- "0.0.1"
6
+ "0.0.2"
7
7
  end
8
8
  end
@@ -0,0 +1,15 @@
1
+ # -*- coding: UTF-8 -*-
2
+
3
+ require "yop/bootstrap"
4
+ require_relative "test_utils"
5
+
6
+ class YopBootstrapTests < YopTestCase
7
+
8
+ # == #bootstrap == #
9
+
10
+ def test_bootstrap_raise_if_template_doesnt_exist
11
+ assert_raise(NonExistentTemplate) do
12
+ Yop.bootstrap("dont-exist", Yop.home)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,47 @@
1
+ # -*- coding: UTF-8 -*-
2
+
3
+ require "yop/config"
4
+ require_relative "test_utils"
5
+
6
+ class YopConfigTests < YopTestCase
7
+ def setup
8
+ super
9
+ Yop.init
10
+ Yop.reload!
11
+ end
12
+
13
+ # == #config == #
14
+
15
+ def test_default_empty_config
16
+ assert_equal({}, Yop.config)
17
+ end
18
+
19
+ def test_default_empty_config_with_key
20
+ assert_nil Yop.config(:foo)
21
+ end
22
+
23
+ # == #config! == #
24
+
25
+ def test_config_bang_set_value
26
+ assert_nil Yop.config(:foo)
27
+ Yop.config! :foo => 42
28
+ assert_equal 42, Yop.config(:foo)
29
+ end
30
+
31
+ def test_config_bang_set_multiple_values
32
+ assert_nil Yop.config(:foo)
33
+ Yop.config! :foo => 42, :bar => "foo"
34
+ assert_equal 42, Yop.config(:foo)
35
+ assert_equal "foo", Yop.config(:bar)
36
+ end
37
+
38
+ # == #reload! == #
39
+
40
+ def test_config_bang_set_value_preserved_after_reload
41
+ assert_nil Yop.config(:foo)
42
+ Yop.config! :foo => 42
43
+ Yop.reload!
44
+ assert_equal 42, Yop.config(:foo)
45
+
46
+ end
47
+ end
@@ -0,0 +1,7 @@
1
+ # -*- coding: UTF-8 -*-
2
+
3
+ require "test/unit"
4
+ require "yop"
5
+
6
+ class YopExceptionsTests < Test::Unit::TestCase
7
+ end
@@ -0,0 +1,35 @@
1
+ # -*- coding: UTF-8 -*-
2
+
3
+ require "yop/home"
4
+ require_relative "test_utils"
5
+
6
+ class YopHomeTests < YopTestCase
7
+
8
+ # == #home == #
9
+
10
+ def test_home_no_subcomponent
11
+ assert_equal @basepath, Yop.home
12
+ end
13
+
14
+ def test_home_subcomponent
15
+ assert_equal "#{@basepath}/foo/bar", Yop.home("foo/bar")
16
+ end
17
+
18
+ # == #init == #
19
+
20
+ def test_init_create_dir
21
+ assert_not_directory @basepath
22
+ Yop.init
23
+ assert_directory @basepath
24
+ end
25
+
26
+ def test_init_create_template_dir
27
+ Yop.init
28
+ assert_directory "#{@basepath}/templates"
29
+ end
30
+
31
+ def test_init_create_config_file
32
+ Yop.init
33
+ assert_true File.file?("#{@basepath}/config.yml")
34
+ end
35
+ end
@@ -0,0 +1,251 @@
1
+ # -*- coding: UTF-8 -*-
2
+
3
+ require "yop/templates"
4
+ require "yop/exceptions"
5
+ require_relative "test_utils"
6
+
7
+ class YopTemplatesTests < YopTestCase
8
+ def setup
9
+ super
10
+ Yop.init
11
+ end
12
+
13
+ # == #templates == #
14
+
15
+ def test_empty_templates_list
16
+ assert_equal [], Yop.templates
17
+ end
18
+
19
+ def test_templates_list
20
+ mkdir "templates/foo"
21
+ mkdir "templates/bar"
22
+
23
+ lst = %w[bar foo].map { |t| "#{Yop.home("templates")}/#{t}" }
24
+
25
+ assert_equal lst, Yop.templates.sort
26
+ end
27
+
28
+ # == #get_template == #
29
+
30
+ def test_get_template_raise_if_doesnt_exist
31
+ assert_raise(NonExistentTemplate) do
32
+ Yop.get_template("dont-exist")
33
+ end
34
+ end
35
+
36
+ def test_get_template_object
37
+ mkdir "templates/foo"
38
+
39
+ assert_instance_of Yop::Template, Yop.get_template("foo")
40
+ end
41
+
42
+ # == Template#apply == #
43
+
44
+ def test_apply_empty_template_on_unexisting_dir
45
+ mkdir "templates/foo"
46
+ t = Yop.get_template("foo")
47
+ mkdir "tmp"
48
+ dest = "#{Yop.home}/tmp/test-foo"
49
+ assert_nothing_raised { t.apply dest }
50
+
51
+ assert_directory dest
52
+ assert_equal [], ls_Ra(dest)
53
+ end
54
+
55
+ def test_apply_empty_template_on_existing_dir
56
+ mkdir "templates/foo"
57
+ t = Yop.get_template("foo")
58
+ mkdir_p "tmp/test-foo"
59
+ dest = "#{Yop.home}/tmp/test-foo"
60
+ assert_nothing_raised { t.apply dest }
61
+ assert_equal [], ls_Ra(dest)
62
+ end
63
+
64
+ def test_apply_empty_template_ignore_temporary_files
65
+ root = "templates/foo"
66
+ mkdir root
67
+ touch "#{root}/foo~"
68
+ touch "#{root}/.bar.swp"
69
+ touch "#{root}/.qux.swo"
70
+ t = Yop.get_template("foo")
71
+ mkdir_p "tmp/test-foo"
72
+ dest = "#{Yop.home}/tmp/test-foo"
73
+ assert_nothing_raised { t.apply dest }
74
+ assert_equal [], ls_Ra(dest)
75
+ end
76
+
77
+ def test_apply_empty_template_ignore_git_dir
78
+ mkdir_p "templates/foo/.git"
79
+ t = Yop.get_template("foo")
80
+ mkdir_p "tmp/test-foo"
81
+ dest = "#{Yop.home}/tmp/test-foo"
82
+ assert_nothing_raised { t.apply dest }
83
+ assert_equal [], ls_Ra(dest)
84
+ end
85
+
86
+ def test_apply_dirs_only
87
+ mkdir_p "templates/foo/bar"
88
+ t = Yop.get_template("foo")
89
+ mkdir_p "tmp/test-foo"
90
+ dest = "#{Yop.home}/tmp/test-foo"
91
+ assert_nothing_raised { t.apply dest }
92
+ assert_directory "#{dest}/bar"
93
+ end
94
+
95
+ def test_apply_dirs_with_empty_file
96
+ mkdir_p "templates/foo/bar"
97
+ touch "templates/foo/barqux"
98
+ t = Yop.get_template("foo")
99
+ mkdir_p "tmp/test-foo"
100
+ dest = "#{Yop.home}/tmp/test-foo"
101
+ assert_nothing_raised { t.apply dest }
102
+ assert_directory "#{dest}/bar"
103
+ assert_true File.file?("#{dest}/barqux")
104
+ end
105
+
106
+ # special files
107
+
108
+ def test_apply_symlink
109
+ mkdir_p "templates/foo/a"
110
+ FileUtils.cd "#{Yop.home}/templates/foo" do
111
+ FileUtils.ln_s "a", "b"
112
+ end
113
+ t = Yop.get_template("foo")
114
+ mkdir_p "tmp/test-foo"
115
+ dest = "#{Yop.home}/tmp/test-foo"
116
+ assert_nothing_raised { t.apply dest }
117
+ assert_directory "#{dest}/a"
118
+ assert_true File.symlink?("#{dest}/b")
119
+ end
120
+
121
+ def test_apply_fifo
122
+ mkdir_p "templates/foo"
123
+ mkfifo "templates/foo/pipe"
124
+ t = Yop.get_template("foo")
125
+ mkdir_p "tmp/test-foo"
126
+ dest = "#{Yop.home}/tmp/test-foo"
127
+ assert_raise(UnsupportedFileType) { t.apply dest }
128
+ end
129
+
130
+ # permissions
131
+
132
+ def test_apply_executable_file
133
+ mkdir_p "templates/foo"
134
+ touch "templates/foo/exe"
135
+ chmod 0744, "templates/foo/exe"
136
+ assert_equal 0100744, File.new("#{Yop.home}/templates/foo/exe").stat.mode
137
+ t = Yop.get_template("foo")
138
+ mkdir_p "tmp/test-foo"
139
+ dest = "#{Yop.home}/tmp/test-foo"
140
+ assert_nothing_raised { t.apply dest }
141
+ assert_equal 0100744, File.new("#{dest}/exe").stat.mode
142
+ end
143
+
144
+ def test_apply_symlink_dont_fail_if_cannot_set_permissions
145
+ unimplement_lchmod!
146
+ mkdir_p "templates/foo"
147
+ touch "templates/foo/a"
148
+ FileUtils.cd "#{Yop.home}/templates/foo" do
149
+ FileUtils.ln_s "a", "b"
150
+ end
151
+ t = Yop.get_template("foo")
152
+ mkdir_p "tmp/test-foo"
153
+ dest = "#{Yop.home}/tmp/test-foo"
154
+ assert_nothing_raised { t.apply dest }
155
+ end
156
+
157
+ def test_apply_symlink_set_permissions_if_possible
158
+ implement_lchmod!
159
+ mkdir_p "templates/foo"
160
+ touch "templates/foo/a"
161
+ FileUtils.cd "#{Yop.home}/templates/foo" do
162
+ FileUtils.ln_s "a", "b"
163
+ end
164
+ t = Yop.get_template("foo")
165
+ mkdir_p "tmp/test-foo"
166
+ dest = "#{Yop.home}/tmp/test-foo"
167
+ assert_nothing_raised { t.apply dest }
168
+ assert_equal 2, @lchmod_args.length
169
+ assert_equal "b", @lchmod_args[-1]
170
+ end
171
+
172
+ # TODO test dir permissions
173
+
174
+ # placeholders in paths
175
+
176
+ def test_apply_dir_with_variable_placeholder_string
177
+ mkdir_p "templates/foo/{(SOME_VAR)}"
178
+ t = Yop.get_template("foo")
179
+ t["SOME_VAR"] = "barqux"
180
+ dest = "#{Yop.home}/tmp/test-foo"
181
+ assert_nothing_raised { t.apply dest }
182
+ assert_directory "#{dest}/barqux"
183
+ assert_not_directory "#{dest}/{(SOME_VAR)}"
184
+ end
185
+
186
+ def test_apply_dir_with_variable_placeholder_symbol
187
+ mkdir_p "templates/foo/{(SOME_VAR)}"
188
+ t = Yop.get_template("foo")
189
+ t[:SOME_VAR] = "barqux"
190
+ dest = "#{Yop.home}/tmp/test-foo"
191
+ assert_nothing_raised { t.apply dest }
192
+ assert_directory "#{dest}/barqux"
193
+ assert_not_directory "#{dest}/{(SOME_VAR)}"
194
+ end
195
+
196
+ def test_apply_dir_with_variable_placeholder_symbol_from_config
197
+ capture_output!
198
+ set_input "not-barqux"
199
+
200
+ mkdir_p "templates/foo/{(SOME_VAR)}"
201
+ Yop.config! :vars => {:SOME_VAR => "barqux"}
202
+ t = Yop.get_template("foo")
203
+ dest = "#{Yop.home}/tmp/test-foo"
204
+ assert_nothing_raised { t.apply dest }
205
+ assert_directory "#{dest}/barqux"
206
+ assert_not_directory "#{dest}/not-barqux"
207
+ assert_not_directory "#{dest}/{(SOME_VAR)}"
208
+ end
209
+
210
+ # placeholders in files
211
+
212
+ def test_apply_file_with_variable_placeholders_in_content
213
+ mkdir_p "templates/foo"
214
+ name = "afoo"
215
+ author = "abar"
216
+ File.open("#{Yop.home}/templates/foo/README", "w") do |f|
217
+ f.write <<-EOS
218
+ # {(NAME)}
219
+
220
+ This is the project {(NAME)} by {(AUTHOR)}.
221
+ EOS
222
+ end
223
+ t = Yop.get_template("foo")
224
+ t["NAME"] = name
225
+ t["AUTHOR"] = author
226
+ dest = "#{Yop.home}/tmp/test-foo"
227
+ assert_nothing_raised { t.apply dest }
228
+ expected = <<-EOS
229
+ # #{name}
230
+
231
+ This is the project #{name} by #{author}.
232
+ EOS
233
+ assert_true File.file?("#{dest}/README")
234
+ assert_equal expected, File.read("#{dest}/README")
235
+ end
236
+
237
+ # UI variables
238
+
239
+ def test_apply_dir_with_var_in_path_values_from_terminal
240
+ capture_output!
241
+ set_input "barqux"
242
+ mkdir_p "templates/foo/{(SOME_VAR)}"
243
+ t = Yop.get_template("foo")
244
+ dest = "#{Yop.home}/tmp/test-foo"
245
+ assert_nothing_raised { t.apply dest }
246
+ assert_directory "#{dest}/barqux"
247
+ assert_not_directory "#{dest}/{(SOME_VAR)}"
248
+ assert_include read_output, "SOME_VAR"
249
+ end
250
+
251
+ end
@@ -0,0 +1,103 @@
1
+ # -*- coding: UTF-8 -*-
2
+
3
+ require "fileutils"
4
+ require "tmpdir"
5
+ require "stringio"
6
+
7
+ require "yop"
8
+
9
+ # An enhanced TestCase class for Yop
10
+ class YopTestCase < Test::Unit::TestCase
11
+ def setup
12
+ @old_basepath = Yop.basepath
13
+ @basepath = Yop.basepath = "#{Dir.mktmpdir}/.yop"
14
+
15
+ @_stdin = $stdin
16
+ @_stdout = $stdout
17
+ @_stderr = $stderr
18
+
19
+ @pwd = Dir.pwd
20
+
21
+ @_lchmod = File.public_method(:lchmod)
22
+ @lchmod_args = nil
23
+ end
24
+
25
+ def teardown
26
+ Yop.basepath = @old_basepath
27
+ FileUtils.rm_rf @basepath
28
+
29
+ $stdin = @_stdin
30
+ $stdout = @_stdout
31
+ $stderr = @_stderr
32
+
33
+ FileUtils.cd @pwd
34
+
35
+ File.define_singleton_method :lchmod, @_lchmod
36
+ end
37
+
38
+ def capture_output!
39
+ $stdout = StringIO.new
40
+ end
41
+
42
+ def set_input(text)
43
+ $stdin = StringIO.new
44
+ $stdin.write text
45
+ $stdin.rewind
46
+ end
47
+
48
+ def read_output
49
+ $stdout.rewind
50
+ $stdout.read
51
+ end
52
+
53
+ # some systems don't implement File.lchmod
54
+ def unimplement_lchmod!
55
+ File.define_singleton_method(:lchmod) { |*_| fail NotImplementedError }
56
+ end
57
+
58
+ def implement_lchmod!
59
+ s = self
60
+ File.define_singleton_method(:lchmod) { |*args| s.set_lchmod_args args }
61
+ end
62
+
63
+ def set_lchmod_args args
64
+ @lchmod_args = args
65
+ end
66
+
67
+ def assert_directory path
68
+ assert_true File.directory?(path)
69
+ end
70
+
71
+ def assert_not_directory path
72
+ assert_false File.directory?(path)
73
+ end
74
+
75
+ # ls -R -a
76
+ def ls_Ra path
77
+ Dir["#{path}/**/*", "#{path}/**/.*"].reject do |d|
78
+ # reject . & .. (for Ruby 1.9)
79
+ d =~ %r(/\.\.?$) && File.directory?(d)
80
+ end
81
+ end
82
+
83
+ # proxy FileUtils methods by prepending a @basepath
84
+ [:mkdir, :mkdir_p, :touch].each do |name|
85
+ define_method(name) do |path|
86
+ FileUtils.send(name, "#{@basepath}/#{path}")
87
+ end
88
+ end
89
+
90
+ def mkfifo path
91
+ system "mkfifo", "#{@basepath}/#{path}"
92
+ end
93
+
94
+ def chmod(mode, path)
95
+ FileUtils.chmod mode, "#{@basepath}/#{path}"
96
+ end
97
+
98
+ def system(*args)
99
+ ret = Kernel.system(*args)
100
+ assert_true $?.success?
101
+ ret
102
+ end
103
+ end
data/tests/tests.rb CHANGED
@@ -1,32 +1,21 @@
1
1
  #! /usr/bin/env ruby
2
2
  # -*- coding: UTF-8 -*-
3
3
 
4
- require "coveralls"
5
- Coveralls.wear!
6
-
7
4
  require "test/unit"
8
5
  require "simplecov"
9
6
 
10
7
  test_dir = File.expand_path(File.dirname(__FILE__))
11
8
 
12
- SimpleCov.formatter = Coveralls::SimpleCov::Formatter
9
+ if ENV["TRAVIS"]
10
+ require "coveralls"
11
+ Coveralls.wear!
12
+ SimpleCov.formatter = Coveralls::SimpleCov::Formatter
13
+ end
13
14
  SimpleCov.start { add_filter "/tests/" }
14
15
 
15
- require "yop"
16
-
17
16
  for t in Dir[File.join(test_dir, "*_tests.rb")]
18
17
  require t
19
18
  end
20
19
 
21
- class YopTests < Test::Unit::TestCase
22
-
23
- # == #version == #
24
-
25
- def test_version
26
- assert(Yop.version =~ /^\d+\.\d+\.\d+/)
27
- end
28
-
29
- end
30
-
31
20
 
32
21
  exit Test::Unit::AutoRunner.run
data/tests/ui_tests.rb ADDED
@@ -0,0 +1,42 @@
1
+ # -*- coding: UTF-8 -*-
2
+
3
+ require "yop/ui"
4
+ require "yop/exceptions"
5
+ require_relative "test_utils"
6
+
7
+ class YopUITests < YopTestCase
8
+ def setup
9
+ super
10
+ @term = Yop::TerminalUI.new
11
+ end
12
+
13
+ def test_base_ui_get_variable_raise_exception
14
+ ui = Yop::UI.new
15
+
16
+ assert_raise(UndefinedTemplateVariable) { ui.get_var "" }
17
+ assert_raise(UndefinedTemplateVariable) { ui.get_var "foo" }
18
+ assert_raise(UndefinedTemplateVariable) { ui.get_var :foo }
19
+ end
20
+
21
+ def test_term_ui_print_var_name
22
+ capture_output!
23
+ set_input "value"
24
+
25
+ assert_nothing_raised { @term.get_var "bar" }
26
+ assert_equal "bar = ", read_output
27
+ end
28
+
29
+ def test_term_ui_raise_if_cant_read
30
+ capture_output!
31
+ set_input ""
32
+
33
+ assert_raise(UndefinedTemplateVariable) { @term.get_var "foo" }
34
+ end
35
+
36
+ def test_term_ui_chomp_value
37
+ capture_output!
38
+ value = "something"
39
+ set_input "#{value}\n"
40
+ assert_equal value, @term.get_var("foo")
41
+ end
42
+ end
@@ -0,0 +1,14 @@
1
+ # -*- coding: UTF-8 -*-
2
+
3
+ require "test/unit"
4
+ require "yop/version"
5
+
6
+ class YopVersionTests < Test::Unit::TestCase
7
+
8
+ # == #version == #
9
+
10
+ def test_version
11
+ assert(Yop.version =~ /^\d+\.\d+\.\d+/)
12
+ end
13
+
14
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Baptiste Fontaine
@@ -30,7 +30,7 @@ cert_chain:
30
30
  QnOQb8zHyNL+gq2m/mnZGrSehx+6AujokjOfHbmivYMfDATOQQx0eIBI18IhacZm
31
31
  42WxhhIV2bwDtd77
32
32
  -----END CERTIFICATE-----
33
- date: 2015-01-31 00:00:00.000000000 Z
33
+ date: 2015-02-11 00:00:00.000000000 Z
34
34
  dependencies:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: trollop
@@ -130,6 +130,20 @@ dependencies:
130
130
  - - "~>"
131
131
  - !ruby/object:Gem::Version
132
132
  version: '0.28'
133
+ - !ruby/object:Gem::Dependency
134
+ name: rubygems-tasks
135
+ requirement: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - "~>"
138
+ - !ruby/object:Gem::Version
139
+ version: 0.2.4
140
+ type: :development
141
+ prerelease: false
142
+ version_requirements: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - "~>"
145
+ - !ruby/object:Gem::Version
146
+ version: 0.2.4
133
147
  description: Yop bootstraps your projects from pre-defined templates
134
148
  email: b@ptistefontaine.fr
135
149
  executables:
@@ -144,8 +158,17 @@ files:
144
158
  - lib/yop/exceptions.rb
145
159
  - lib/yop/home.rb
146
160
  - lib/yop/templates.rb
161
+ - lib/yop/ui.rb
147
162
  - lib/yop/version.rb
163
+ - tests/bootstrap_tests.rb
164
+ - tests/config_tests.rb
165
+ - tests/exceptions_tests.rb
166
+ - tests/home_tests.rb
167
+ - tests/templates_tests.rb
168
+ - tests/test_utils.rb
148
169
  - tests/tests.rb
170
+ - tests/ui_tests.rb
171
+ - tests/version_tests.rb
149
172
  homepage: https://github.com/bfontaine/Yop
150
173
  licenses:
151
174
  - MIT
@@ -171,5 +194,12 @@ signing_key:
171
194
  specification_version: 4
172
195
  summary: Templates-based projects manager
173
196
  test_files:
197
+ - tests/bootstrap_tests.rb
198
+ - tests/config_tests.rb
199
+ - tests/exceptions_tests.rb
200
+ - tests/home_tests.rb
201
+ - tests/templates_tests.rb
202
+ - tests/test_utils.rb
174
203
  - tests/tests.rb
175
- has_rdoc:
204
+ - tests/ui_tests.rb
205
+ - tests/version_tests.rb
metadata.gz.sig CHANGED
Binary file