rstub 0.2.1 → 0.3.1

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: 44dda35b2036924e4e4d798aa8a3ca55b5dbe929
4
- data.tar.gz: 6fd78aa063a60cad82e874cf0707438e09303e8c
3
+ metadata.gz: a13ff1120aa1b35ea344951de2522d50a45bc7aa
4
+ data.tar.gz: 8a6c09dd608324da3045365e2248fb745f77238e
5
5
  SHA512:
6
- metadata.gz: a20122de6d8fd7864763532bfd961d6046ad0a32ce85dcb3744b66d02034af5c931485afeb853246ba0fc800738811cbbc7f84e82824f50b53d30fb30362cbe2
7
- data.tar.gz: 55e9ce7005f0667e72d2f1a83e86ae0d4ed6cb6770825d6a12c79da8571543d183cb902af1ac83eb4b45c7f9dfbda6f70297464fafa735687affdba4ec1bafc2
6
+ metadata.gz: 31e43a4bae4b24445b4c2c5a76bab16b2fe97c92209fb665f61e3bc84e7788686536d6807721a8296ede19fd39a07faaab9a5c123e5a7126ea336727aa02dcb8
7
+ data.tar.gz: 92d5d4083b05abe79052ad4894d8246bf1e332f6006a85c92ab27b5dce90d9636f15ed3ccf2adc4157295a19babed7344a8eb957f9e5d598c49fa9e3dbcdbb31
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- ruby '2.2.2'
3
+ ruby '2.3.1'
4
4
 
5
5
  gemspec
6
6
 
data/README.md CHANGED
@@ -7,13 +7,10 @@
7
7
  Run `gem install rstub`
8
8
 
9
9
  ## About
10
- RStub was created for [CIS 196](http://www.seas.upenn.edu/~cis196/) to generate
11
- boilerplate homework for students to fill out. It was inspired from the
12
- [Stubbify](https://github.com/isibner/stubbify) module.
10
+ RStub was created for [CIS 196](http://www.seas.upenn.edu/~cis196/) to generate boilerplate homework for students to fill out. It was inspired from the [Stubbify](https://github.com/isibner/stubbify) module.
13
11
 
14
12
  ## Usage
15
- Running `rstub homework.rb studentHW` on homework.rb, which has the following
16
- code:
13
+ Running `rstub homework.rb studentHW` on homework.rb, which has the following code:
17
14
  ```ruby
18
15
  def add (a, b)
19
16
  # add the two input variables
@@ -30,15 +27,13 @@ def add (a, b)
30
27
  end
31
28
  ```
32
29
 
33
- Any number of files can be provided, and files can be glob patterns like `*/*.rb`.
34
- The last argument must be the test directory.
30
+ Any number of files can be provided, and files can be glob patterns like `*/*.rb`. The last argument must be the test directory.
35
31
 
36
- RStub preserves the relative paths of the stubbed files, so `./foo/bar.rb` will
37
- be copied and stubbed into `./targetDir/foo/bar.rb`.
32
+ RStub preserves the relative paths of the stubbed files, so `./foo/bar.rb` will be copied and stubbed into `./targetDir/foo/bar.rb`.
38
33
 
39
34
  ```
40
35
  $ rstub rb/homeworkFile1.rb rb/homeworkFile2.rb rb/util/homeworkUtil.rb student-homework
41
- $ rstub "rb/**/*.rb" student-homework # equivalent with glob pattern
36
+ $ rstub "rb/*" student-homework # equivalent with glob pattern
42
37
  $ tree .
43
38
  .
44
39
  ├── rb
data/lib/rstub.rb CHANGED
@@ -14,7 +14,7 @@ class RStub
14
14
  end
15
15
 
16
16
  def start(args = [])
17
- fail ArgumentError, 'Not enough arguments' if args.size < 2
17
+ raise ArgumentError, 'Not enough arguments' if args.size < 2
18
18
  parse_args(args)
19
19
  make_new_directory_structure
20
20
  parse_files
@@ -23,7 +23,7 @@ class RStub
23
23
  private
24
24
 
25
25
  def directory?(directory)
26
- !/^\.?\w+$/.match(directory).nil?
26
+ directory != '*' && /\.\w+$/.match(directory).nil?
27
27
  end
28
28
 
29
29
  # returns a hash with a files key with a value of an array of the files to be
@@ -32,7 +32,7 @@ class RStub
32
32
  def parse_args(args)
33
33
  self.target = args.pop
34
34
  unless directory?(target)
35
- fail ArgumentError, 'The last argument needs to be a directory'
35
+ raise ArgumentError, 'The last argument needs to be a directory'
36
36
  end
37
37
  self.files = args
38
38
  end
@@ -1,7 +1,10 @@
1
1
  # FileParser goes through a file and writes the contents of it to a target file,
2
2
  # but it ignores anything between the delimiters # STUB and # ENDSTUB.
3
3
  class FileParser
4
+ attr_accessor :stub_regex, :end_stub_regex, :stubbing
5
+
4
6
  def stub(target_file, file)
7
+ determine_delimiters(file)
5
8
  File.open(file, 'r') do |readable_file|
6
9
  File.open(target_file, 'w') do |target|
7
10
  write_text(target, readable_file)
@@ -11,22 +14,34 @@ class FileParser
11
14
 
12
15
  private
13
16
 
14
- def start_stubbing?(line, stubbing)
15
- return true if /#\s*stub\s*/i.match(line)
16
- stubbing
17
+ def start_stubbing?(line)
18
+ self.stubbing = true if line.valid_encoding? && stub_regex.match(line)
17
19
  end
18
20
 
19
- def end_stubbing?(line, stubbing)
20
- return false if /#\s*endstub\s*/i.match(line)
21
- stubbing
21
+ def end_stubbing?(line)
22
+ self.stubbing = false if line.valid_encoding? && end_stub_regex.match(line)
22
23
  end
23
24
 
24
25
  def write_text(target, readable_file)
25
- stubbing = false
26
+ self.stubbing = false
26
27
  IO.foreach(readable_file) do |line|
27
- stubbing = start_stubbing?(line, stubbing)
28
+ start_stubbing?(line)
28
29
  target.puts line unless stubbing
29
- stubbing = end_stubbing?(line, stubbing)
30
+ end_stubbing?(line)
30
31
  end
31
32
  end
33
+
34
+ def determine_delimiters(file)
35
+ self.stub_regex, self.end_stub_regex = if /\w+\.rb/i =~ file
36
+ [/#\s*stub\s*/i,
37
+ /#\s*endstub\s*/i]
38
+ elsif /\w+\.html/i =~ file
39
+ [/<!--\s+STUB\s+-->/i,
40
+ /<!--\s+ENDSTUB\s+-->/i]
41
+ else
42
+ # will never be matched by anything
43
+ # thereby preserving all text
44
+ [/$a/i, /$a/i]
45
+ end
46
+ end
32
47
  end
@@ -1,5 +1,5 @@
1
- # PathParser parses directory and file paths into a hash with directory and file
2
- # arrays.
1
+ # PathParser parses directory and file paths into a hash with directory
2
+ # and file arrays.
3
3
  class PathParser
4
4
  def get_globs(files)
5
5
  glob_files = check_globs(files)
@@ -24,18 +24,12 @@ class PathParser
24
24
  !%r{\/}.match(file).nil?
25
25
  end
26
26
 
27
- def extract_directories(file)
28
- file.split('/').slice(0...-1)
29
- end
30
-
31
27
  def get_directories(files)
32
28
  dirs = []
33
29
  files.each do |file|
34
30
  if Dir.exist? file
35
31
  dirs << file
36
32
  dirs.concat(get_directories(Dir.glob("#{file}/*")))
37
- elsif in_directory?(file)
38
- dirs << extract_directories(file)
39
33
  end
40
34
  end
41
35
  dirs.flatten.uniq
data/rstub.gemspec CHANGED
@@ -1,8 +1,8 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'rstub'
3
- s.version = '0.2.1'
3
+ s.version = '0.3.1'
4
4
 
5
- s.date = '2015-05-30'
5
+ s.date = '2016-08-07'
6
6
  s.summary = 'A gem to stub out code'
7
7
  s.description = 'A gem to stub out pieces of code'
8
8
  s.authors = 'Justin Kim'
@@ -76,5 +76,21 @@ describe FileParser do
76
76
  expect_text('file1.rb', "hello\nfoo\n")
77
77
  end
78
78
  end
79
+
80
+ describe 'can stub html files' do
81
+ it 'can add text without stubs' do
82
+ text = "foo\nbar\n"
83
+ add_text('file1.html.erb', text)
84
+ stub_all
85
+ expect_text('file1.html.erb', "foo\nbar\n")
86
+ end
87
+
88
+ it 'doesn\'t include text between stub delimiters' do
89
+ text = "<!-- STUB -->\n hello <!-- ENDSTUB -->\n"
90
+ add_text('file1.html.erb', text)
91
+ stub_all
92
+ expect_text('file1.html.erb', '')
93
+ end
94
+ end
79
95
  end
80
96
  end
@@ -1,37 +1,55 @@
1
1
  describe PathParser do
2
2
  describe '#get_globs' do
3
- before :all do
4
- Dir.mkdir 'test_dir/nested_dir'
5
- File.new 'test_dir/nested_dir/nested_file2.rb', 'w+'
6
- end
3
+ let(:path_parser) { PathParser.new }
7
4
 
8
- after :all do
9
- FileUtils.rm_r 'test_dir/nested_dir' if Dir.exist? 'test_dir/nested_dir'
10
- end
5
+ context 'one nested directory' do
6
+ before :all do
7
+ Dir.mkdir 'dir1/nested_dir'
8
+ File.new 'dir1/nested_dir/nested_file2.rb', 'w+'
9
+ end
11
10
 
12
- let(:path_parser) { PathParser.new }
11
+ after :all do
12
+ FileUtils.rm_r 'dir1/nested_dir' if Dir.exist? 'dir1/nested_dir'
13
+ end
13
14
 
14
- it 'returns the same array when there is no match' do
15
- expect(path_parser.get_globs(['file1.rb'])[:files])
16
- .to match_array(['file1.rb'])
17
- end
15
+ it 'returns the same array when there is no match' do
16
+ expect(path_parser.get_globs(['file1.rb'])[:files])
17
+ .to match_array(['file1.rb'])
18
+ end
18
19
 
19
- it 'returns glob matches from current directory' do
20
- paths = path_parser.get_globs(['*'])
21
- expect(paths[:directories])
22
- .to match_array(%w(test_dir nested_dir test_dir/nested_dir))
23
- expect(paths[:files])
24
- .to match_array(%w(file1.rb file2.rb test_dir/nested_file.rb
25
- test_dir/nested_dir/nested_file2.rb))
20
+ it 'returns glob matches from current directory' do
21
+ paths = path_parser.get_globs(['*'])
22
+ expect(paths[:directories])
23
+ .to match_array(%w(dir1 dir1/nested_dir))
24
+ expect(paths[:files])
25
+ .to match_array(%w(file1.rb file2.rb dir1/nested_file1.rb
26
+ dir1/nested_dir/nested_file2.rb))
27
+ end
26
28
  end
27
29
 
28
- it 'returns glob matches from lowel directory' do
29
- paths = path_parser.get_globs(['*/*'])
30
- expect(paths[:directories])
31
- .to match_array(%w(test_dir nested_dir test_dir/nested_dir))
32
- expect(paths[:files])
33
- .to match_array(%w(test_dir/nested_file.rb
34
- test_dir/nested_dir/nested_file2.rb))
30
+ context 'two nested directories' do
31
+ before :all do
32
+ Dir.mkdir 'dir1/nested_dir'
33
+ Dir.mkdir 'dir1/nested_dir/nested_dir2'
34
+ File.new 'dir1/nested_file1.rb'
35
+ File.new 'dir1/nested_dir/nested_file2.rb', 'w+'
36
+ File.new 'dir1/nested_dir/nested_dir2/nested_file3.rb', 'w+'
37
+ end
38
+
39
+ after :all do
40
+ FileUtils.rm_r 'dir1/nested_dir' if Dir.exist? 'dir1/nested_dir'
41
+ end
42
+
43
+ it 'returns glob matches even with two nested directories' do
44
+ paths = path_parser.get_globs(['*'])
45
+ expect(paths[:directories])
46
+ .to match_array(%w(dir1 dir1/nested_dir dir1/nested_dir/nested_dir2))
47
+ expect(paths[:files])
48
+ .to match_array(%w(file1.rb file2.rb
49
+ dir1/nested_file1.rb
50
+ dir1/nested_dir/nested_file2.rb
51
+ dir1/nested_dir/nested_dir2/nested_file3.rb))
52
+ end
35
53
  end
36
54
  end
37
55
  end
data/spec/rstub_spec.rb CHANGED
@@ -53,8 +53,8 @@ describe RStub do
53
53
  expect(Dir.exist?('target')).to be true
54
54
  expect(File.exist?('target/file1.rb')).to be true
55
55
  expect(File.exist?('target/file2.rb')).to be true
56
- expect(Dir.exist?('target/test_dir')).to be true
57
- expect(File.exist?('target/test_dir/nested_file.rb')).to be true
56
+ expect(Dir.exist?('target/dir1')).to be true
57
+ expect(File.exist?('target/dir1/nested_file1.rb')).to be true
58
58
  end
59
59
  end
60
60
  end
data/spec/spec_helper.rb CHANGED
@@ -11,10 +11,11 @@ Dir[File.expand_path('../../lib/rstub/*.rb', __FILE__)].each { |f| require f }
11
11
  RSpec.configure do |config|
12
12
  config.before :all do
13
13
  Dir.chdir('spec/fixtures')
14
- Dir.mkdir 'test_dir' unless Dir.exist? 'test_dir'
14
+ Dir.glob('*').each { |f| FileUtils.rm_r f }
15
+ Dir.mkdir 'dir1' unless Dir.exist? 'dir1'
15
16
  File.new 'file1.rb', 'w+'
16
17
  File.new 'file2.rb', 'w+'
17
- File.new 'test_dir/nested_file.rb', 'w+'
18
+ File.new 'dir1/nested_file1.rb', 'w+'
18
19
  end
19
20
 
20
21
  config.after :each do
@@ -22,7 +23,7 @@ RSpec.configure do |config|
22
23
  end
23
24
 
24
25
  config.after :all do
25
- FileUtils.rm_r 'test_dir' if Dir.exist? 'test_dir'
26
+ FileUtils.rm_r 'dir1' if Dir.exist? 'dir1'
26
27
  FileUtils.rm_r 'file1.rb'
27
28
  FileUtils.rm_r 'file2.rb'
28
29
  Dir.chdir('../..')
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rstub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Kim
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-30 00:00:00.000000000 Z
11
+ date: 2016-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -82,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
82
  version: '0'
83
83
  requirements: []
84
84
  rubyforge_project:
85
- rubygems_version: 2.4.8
85
+ rubygems_version: 2.6.6
86
86
  signing_key:
87
87
  specification_version: 4
88
88
  summary: A gem to stub out code