nrename 0.2.0 → 0.2.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.
- data/README.md +2 -0
- data/features/options_processing.feature +1 -1
- data/features/renaming_files.feature +67 -16
- data/features/support/env.rb +5 -17
- data/lib/nrename/directory.rb +33 -5
- data/lib/nrename/numbered_file.rb +10 -5
- data/lib/nrename/options.rb +18 -3
- data/lib/nrename/version.rb +1 -1
- data/spec/nrename/directory_spec.rb +61 -17
- data/spec/nrename/numbered_file_spec.rb +9 -11
- data/spec/nrename/options_spec.rb +98 -14
- metadata +19 -3
data/README.md
CHANGED
|
@@ -67,6 +67,8 @@ Options:
|
|
|
67
67
|
-X, --execute Do actual work
|
|
68
68
|
-R, --recursive Process given directories recursively
|
|
69
69
|
-N, --numbers-only Leave only numbers in file name
|
|
70
|
+
-D, --rename-dirs Rename only directories instead of regular files
|
|
71
|
+
--renumber Renumber files from starting from 1 and on
|
|
70
72
|
--regexp REGEXP Use REGEXP to match filenames
|
|
71
73
|
-v, --[no-]verbose Run verbosely
|
|
72
74
|
-h, --help Display this message
|
|
@@ -28,7 +28,7 @@ Feature: Options processing
|
|
|
28
28
|
Then the exit status should be 1
|
|
29
29
|
And the stderr should contain "no_dir is not a valid directory."
|
|
30
30
|
|
|
31
|
-
Scenario: Passing file instead of directory
|
|
31
|
+
Scenario: Passing regular file instead of directory
|
|
32
32
|
Given an empty file named "foo"
|
|
33
33
|
When I run `nrename foo`
|
|
34
34
|
Then the exit status should be 1
|
|
@@ -3,7 +3,7 @@ Feature: Renaming files
|
|
|
3
3
|
As a user
|
|
4
4
|
I want program to rename files correctly
|
|
5
5
|
|
|
6
|
-
Scenario: Renaming files in specified directory
|
|
6
|
+
Scenario: Renaming regular files in specified directory
|
|
7
7
|
Given a directory named "dir"
|
|
8
8
|
And the following empty files inside directory "dir":
|
|
9
9
|
| 1.txt |
|
|
@@ -14,11 +14,28 @@ Feature: Renaming files
|
|
|
14
14
|
When I run `nrename -X dir`
|
|
15
15
|
Then the exit status should be 0
|
|
16
16
|
And the following files should exist inside directory "dir":
|
|
17
|
-
| 001.txt
|
|
18
|
-
| 010.txt
|
|
19
|
-
| 023.txt
|
|
20
|
-
| 101.txt
|
|
21
|
-
| 200.txt
|
|
17
|
+
| 001.txt |
|
|
18
|
+
| 010.txt |
|
|
19
|
+
| 023.txt |
|
|
20
|
+
| 101.txt |
|
|
21
|
+
| 200.txt |
|
|
22
|
+
|
|
23
|
+
Scenario: Renaming directories in specified directory
|
|
24
|
+
Given a directory named "dir"
|
|
25
|
+
And the following directories inside directory "dir":
|
|
26
|
+
| 1 |
|
|
27
|
+
| 10 |
|
|
28
|
+
| 0023 |
|
|
29
|
+
| 101 |
|
|
30
|
+
| 200 |
|
|
31
|
+
When I run `nrename -DX dir`
|
|
32
|
+
Then the exit status should be 0
|
|
33
|
+
And the following directories should exist inside directory "dir":
|
|
34
|
+
| 001 |
|
|
35
|
+
| 010 |
|
|
36
|
+
| 023 |
|
|
37
|
+
| 101 |
|
|
38
|
+
| 200 |
|
|
22
39
|
|
|
23
40
|
Scenario: Dry-run
|
|
24
41
|
Given a directory named "dir"
|
|
@@ -35,23 +52,23 @@ Feature: Renaming files
|
|
|
35
52
|
| 10.txt |
|
|
36
53
|
| 0023.txt |
|
|
37
54
|
And the following files should not exist:
|
|
38
|
-
| 01.txt
|
|
39
|
-
| 23.txt
|
|
55
|
+
| 01.txt |
|
|
56
|
+
| 23.txt |
|
|
40
57
|
|
|
41
58
|
Scenario: Renaming files in multiple directories
|
|
42
59
|
Given a directory named "foo"
|
|
43
60
|
And a directory named "bar"
|
|
44
61
|
And the following empty files inside directory "foo":
|
|
45
|
-
| 1.txt
|
|
46
|
-
| 010.txt
|
|
62
|
+
| 1.txt |
|
|
63
|
+
| 010.txt |
|
|
47
64
|
And the following empty files inside directory "bar":
|
|
48
65
|
| 33.txt |
|
|
49
66
|
| 01234.txt |
|
|
50
67
|
And I run `nrename -X foo bar`
|
|
51
68
|
Then the exit status should be 0
|
|
52
69
|
And the following files should exist inside directory "foo":
|
|
53
|
-
| 01.txt
|
|
54
|
-
| 10.txt
|
|
70
|
+
| 01.txt |
|
|
71
|
+
| 10.txt |
|
|
55
72
|
And the following files should exist inside directory "bar":
|
|
56
73
|
| 0033.txt |
|
|
57
74
|
| 1234.txt |
|
|
@@ -60,16 +77,16 @@ Feature: Renaming files
|
|
|
60
77
|
Given a directory named "foo"
|
|
61
78
|
And a directory named "bar"
|
|
62
79
|
And the following empty files inside directory "foo":
|
|
63
|
-
| 1.txt
|
|
64
|
-
| 010.txt
|
|
80
|
+
| 1.txt |
|
|
81
|
+
| 010.txt |
|
|
65
82
|
And the following empty files inside directory "bar":
|
|
66
83
|
| 33.txt |
|
|
67
84
|
| 01234.txt |
|
|
68
85
|
And I run `nrename -XR .`
|
|
69
86
|
Then the exit status should be 0
|
|
70
87
|
And the following files should exist inside directory "foo":
|
|
71
|
-
| 01.txt
|
|
72
|
-
| 10.txt
|
|
88
|
+
| 01.txt |
|
|
89
|
+
| 10.txt |
|
|
73
90
|
And the following files should exist inside directory "bar":
|
|
74
91
|
| 0033.txt |
|
|
75
92
|
| 1234.txt |
|
|
@@ -105,3 +122,37 @@ Feature: Renaming files
|
|
|
105
122
|
| 003_01.txt |
|
|
106
123
|
| 003_10.txt |
|
|
107
124
|
| 003_23.txt |
|
|
125
|
+
|
|
126
|
+
Scenario: Renaming files from scratch
|
|
127
|
+
Given a directory named "dir"
|
|
128
|
+
And the following empty files inside directory "dir":
|
|
129
|
+
| 1.txt |
|
|
130
|
+
| 10.txt |
|
|
131
|
+
| 0023.txt |
|
|
132
|
+
| 101.txt |
|
|
133
|
+
| 200.txt |
|
|
134
|
+
When I run `nrename -X --renumber dir`
|
|
135
|
+
Then the exit status should be 0
|
|
136
|
+
And the following files should exist inside directory "dir":
|
|
137
|
+
| 1.txt |
|
|
138
|
+
| 2.txt |
|
|
139
|
+
| 3.txt |
|
|
140
|
+
| 4.txt |
|
|
141
|
+
| 5.txt |
|
|
142
|
+
|
|
143
|
+
Scenario: Renaming files from scratch in right order
|
|
144
|
+
Given a directory named "dir"
|
|
145
|
+
And the following files with content inside directory "dir":
|
|
146
|
+
| 1.txt | first |
|
|
147
|
+
| 10.txt | second |
|
|
148
|
+
| 0023.txt | third |
|
|
149
|
+
| 101.txt | fourth |
|
|
150
|
+
| 200.txt | fifth |
|
|
151
|
+
When I run `nrename -X --renumber dir`
|
|
152
|
+
Then the exit status should be 0
|
|
153
|
+
And the following files with content should exist inside directory "dir":
|
|
154
|
+
| 1.txt | first |
|
|
155
|
+
| 2.txt | second |
|
|
156
|
+
| 3.txt | third |
|
|
157
|
+
| 4.txt | fourth |
|
|
158
|
+
| 5.txt | fifth |
|
data/features/support/env.rb
CHANGED
|
@@ -3,26 +3,14 @@ require 'fileutils'
|
|
|
3
3
|
|
|
4
4
|
ENV['PATH'] = "#{File.expand_path __FILE__, '../../../bin'}#{File::PATH_SEPARATOR}#{ENV['PATH']}"
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
files.raw.each do |file_row|
|
|
8
|
-
write_file "#{directory}/#{file_row[0]}", ''
|
|
9
|
-
end
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
Then(/^the stdout should match \/([^\/]*)\/$/) do |expected|
|
|
13
|
-
assert_matching_output expected, all_stdout
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
Then(/^the following files should exist inside directory "([^"]*)":$/) do |directory, files|
|
|
17
|
-
files.raw.each do |file_row|
|
|
18
|
-
check_file_presence ["#{directory}/#{file_row[0]}"], true
|
|
19
|
-
end
|
|
20
|
-
end
|
|
6
|
+
application_root = File.expand_path('../../..', __FILE__)
|
|
21
7
|
|
|
8
|
+
# Account for slow JVM startup.
|
|
22
9
|
Before do
|
|
23
|
-
@aruba_timeout_seconds = 10
|
|
10
|
+
@aruba_timeout_seconds = 10
|
|
24
11
|
end
|
|
25
12
|
|
|
13
|
+
# Remove aruba's temporary directory after each scenario.
|
|
26
14
|
After do
|
|
27
|
-
FileUtils.rm_rf File.expand_path
|
|
15
|
+
FileUtils.rm_rf File.expand_path 'tmp', application_root
|
|
28
16
|
end
|
data/lib/nrename/directory.rb
CHANGED
|
@@ -7,20 +7,29 @@ module Nrename
|
|
|
7
7
|
extend Forwardable
|
|
8
8
|
|
|
9
9
|
def_delegator :numbered_files, :empty?
|
|
10
|
-
def_delegator
|
|
10
|
+
def_delegator Nrename, :options
|
|
11
11
|
|
|
12
12
|
def initialize(dir)
|
|
13
13
|
@dir = Pathname.new dir
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
+
def directories
|
|
17
|
+
@dir.children.select &:directory?
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def regular_files
|
|
21
|
+
@dir.children.select &:file?
|
|
22
|
+
end
|
|
23
|
+
|
|
16
24
|
def files
|
|
17
|
-
|
|
25
|
+
options.rename_dirs ? directories : regular_files
|
|
18
26
|
end
|
|
19
27
|
|
|
20
28
|
def numbered_files
|
|
21
29
|
@numbered_files ||= files.
|
|
22
30
|
select { |file| file.basename.to_s =~ options.pattern }.
|
|
23
|
-
map
|
|
31
|
+
map { |file| NumberedFile.new file }.
|
|
32
|
+
sort_by(&:number)
|
|
24
33
|
end
|
|
25
34
|
|
|
26
35
|
def num_field_length
|
|
@@ -28,13 +37,32 @@ module Nrename
|
|
|
28
37
|
end
|
|
29
38
|
|
|
30
39
|
def max_number
|
|
31
|
-
|
|
40
|
+
if options.renumber
|
|
41
|
+
numbered_files.size
|
|
42
|
+
else
|
|
43
|
+
numbered_files.map(&:number).max
|
|
44
|
+
end
|
|
32
45
|
end
|
|
33
46
|
|
|
34
|
-
def
|
|
47
|
+
def renumber
|
|
48
|
+
numbered_files.each_with_index do |file, idx|
|
|
49
|
+
|
|
50
|
+
# Force 1-based indexing (could use Enumerator#with_index with
|
|
51
|
+
# argument, but that would be incompatible with ruby-1.8).
|
|
52
|
+
idx += 1
|
|
53
|
+
|
|
54
|
+
file.normalize num_field_length, idx
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def fix_numbers
|
|
35
59
|
numbered_files.each do |file|
|
|
36
60
|
file.normalize num_field_length
|
|
37
61
|
end
|
|
38
62
|
end
|
|
63
|
+
|
|
64
|
+
def normalize
|
|
65
|
+
options.renumber ? renumber : fix_numbers
|
|
66
|
+
end
|
|
39
67
|
end
|
|
40
68
|
end
|
|
@@ -4,23 +4,28 @@ require 'forwardable'
|
|
|
4
4
|
|
|
5
5
|
module Nrename
|
|
6
6
|
class NumberedFile
|
|
7
|
-
attr_reader :path, :number_length
|
|
7
|
+
attr_reader :path, :number_length, :override_number
|
|
8
8
|
|
|
9
9
|
extend Forwardable
|
|
10
10
|
|
|
11
|
-
def_delegator
|
|
11
|
+
def_delegator Nrename, :options
|
|
12
12
|
|
|
13
13
|
def initialize(path)
|
|
14
14
|
@path = path
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
def number
|
|
18
|
-
path.basename.to_s[options.pattern, 1].to_i
|
|
18
|
+
override_number || path.basename.to_s[options.pattern, 1].to_i
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
def normalize(length)
|
|
21
|
+
def normalize(length, override_number = nil)
|
|
22
22
|
@number_length = length
|
|
23
|
-
|
|
23
|
+
@override_number = override_number
|
|
24
|
+
|
|
25
|
+
rename_to normalized_path
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def rename_to(new_name)
|
|
24
29
|
return if path == new_name
|
|
25
30
|
|
|
26
31
|
FileUtils.mv path, new_name, {
|
data/lib/nrename/options.rb
CHANGED
|
@@ -2,6 +2,7 @@ require 'forwardable'
|
|
|
2
2
|
require 'optparse'
|
|
3
3
|
require 'ostruct'
|
|
4
4
|
require 'set'
|
|
5
|
+
require 'shellwords'
|
|
5
6
|
require 'singleton'
|
|
6
7
|
|
|
7
8
|
module Nrename
|
|
@@ -15,6 +16,8 @@ module Nrename
|
|
|
15
16
|
:execute => false,
|
|
16
17
|
:pattern => /(\d+)/,
|
|
17
18
|
:recursive => false,
|
|
19
|
+
:rename_dirs => false,
|
|
20
|
+
:renumber => false,
|
|
18
21
|
:verbose => true
|
|
19
22
|
}
|
|
20
23
|
end
|
|
@@ -26,6 +29,10 @@ module Nrename
|
|
|
26
29
|
@options ||= OpenStruct.new default_options
|
|
27
30
|
end
|
|
28
31
|
|
|
32
|
+
def reset
|
|
33
|
+
@options = nil
|
|
34
|
+
end
|
|
35
|
+
|
|
29
36
|
def parser
|
|
30
37
|
OptionParser.new do |opts|
|
|
31
38
|
opts.banner = "Usage: #{Nrename.executable_name} [OPTINS] DIR..."
|
|
@@ -47,6 +54,16 @@ module Nrename
|
|
|
47
54
|
options.numbers_only = n
|
|
48
55
|
end
|
|
49
56
|
|
|
57
|
+
opts.on '-D', '--rename-dirs',
|
|
58
|
+
'Rename only directories instead of regular files' do |d|
|
|
59
|
+
options.rename_dirs = d
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
opts.on '--renumber',
|
|
63
|
+
'Renumber files from starting from 1 and on' do |renum|
|
|
64
|
+
options.renumber = renum
|
|
65
|
+
end
|
|
66
|
+
|
|
50
67
|
opts.on '--regexp REGEXP', Regexp,
|
|
51
68
|
'Use REGEXP to match filenames' do |regexp|
|
|
52
69
|
options.pattern = regexp
|
|
@@ -69,8 +86,6 @@ module Nrename
|
|
|
69
86
|
end
|
|
70
87
|
|
|
71
88
|
def parse(args)
|
|
72
|
-
@options = nil
|
|
73
|
-
|
|
74
89
|
# Display help if called through 'nrename' executable and without arguments:
|
|
75
90
|
if args.empty? && Nrename.executable_name == 'nrename'
|
|
76
91
|
args << '--help'
|
|
@@ -104,7 +119,7 @@ module Nrename
|
|
|
104
119
|
|
|
105
120
|
if options.recursive
|
|
106
121
|
dirs.dup.each do |dir|
|
|
107
|
-
Dir.glob(File.join dir, '**/') do |subdir|
|
|
122
|
+
Dir.glob(File.join dir.shellescape, '**/') do |subdir|
|
|
108
123
|
dirs << subdir.chomp('/')
|
|
109
124
|
end
|
|
110
125
|
end
|
data/lib/nrename/version.rb
CHANGED
|
@@ -8,15 +8,71 @@ describe Nrename::Directory do
|
|
|
8
8
|
end
|
|
9
9
|
|
|
10
10
|
describe '#files' do
|
|
11
|
-
|
|
11
|
+
let(:files) { %w[foo bar baz] }
|
|
12
|
+
let(:dirs) { %w[quux corge grault garply] }
|
|
13
|
+
|
|
14
|
+
before :each do
|
|
15
|
+
inside test_dir do
|
|
16
|
+
touch files
|
|
17
|
+
mkdir dirs
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it 'returns regular files when option "rename_dirs" is false' do
|
|
22
|
+
Nrename.options.stub :rename_dirs => false
|
|
23
|
+
|
|
24
|
+
dir = Nrename::Directory.new test_dir
|
|
25
|
+
dir_files = dir.files.map { |file| file.basename.to_s }
|
|
26
|
+
|
|
27
|
+
expect(dir_files).to match_array files
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it 'returns directories when option "rename_dirs" is true' do
|
|
31
|
+
Nrename.options.stub :rename_dirs => true
|
|
32
|
+
|
|
33
|
+
dir = Nrename::Directory.new test_dir
|
|
34
|
+
dir_files = dir.files.map { |file| file.basename.to_s }
|
|
35
|
+
|
|
36
|
+
expect(dir_files).to match_array dirs
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
describe '#directories' do
|
|
41
|
+
it 'returns list of directories in given directory' do
|
|
42
|
+
dirs = %w[1 2 3 4 foo bar baz quux]
|
|
43
|
+
|
|
44
|
+
inside test_dir do
|
|
45
|
+
mkdir dirs
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
dir = Nrename::Directory.new test_dir
|
|
49
|
+
expect(dir).to have(dirs.length).directories
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it 'ignores regular files' do
|
|
12
53
|
files = %w[1.rb 2.rb 3.rb foo.rb bar baz.java]
|
|
54
|
+
dirs = %w[1 2 03 40 55]
|
|
13
55
|
|
|
14
56
|
inside test_dir do
|
|
15
57
|
touch files
|
|
58
|
+
mkdir dirs
|
|
16
59
|
end
|
|
17
60
|
|
|
18
61
|
dir = Nrename::Directory.new test_dir
|
|
19
|
-
expect(dir).to have(
|
|
62
|
+
expect(dir).to have(dirs.length).directories
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
describe '#regular_files' do
|
|
67
|
+
it 'returns all regualr files in given directory' do
|
|
68
|
+
files = %w[1.rb 2.rb 3.rb foo.rb bar baz.java]
|
|
69
|
+
|
|
70
|
+
inside test_dir do
|
|
71
|
+
touch files
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
dir = Nrename::Directory.new test_dir
|
|
75
|
+
expect(dir).to have(files.length).regular_files
|
|
20
76
|
end
|
|
21
77
|
|
|
22
78
|
it 'ignores directories' do
|
|
@@ -29,7 +85,7 @@ describe Nrename::Directory do
|
|
|
29
85
|
end
|
|
30
86
|
|
|
31
87
|
dir = Nrename::Directory.new test_dir
|
|
32
|
-
expect(dir).to have(files.length).
|
|
88
|
+
expect(dir).to have(files.length).regular_files
|
|
33
89
|
end
|
|
34
90
|
end
|
|
35
91
|
|
|
@@ -45,7 +101,7 @@ describe Nrename::Directory do
|
|
|
45
101
|
expect(dir).to have(files.length).numbered_files
|
|
46
102
|
end
|
|
47
103
|
|
|
48
|
-
it 'returns empty
|
|
104
|
+
it 'returns empty collection when there is no numbered files in dir' do
|
|
49
105
|
mkdir test_dir
|
|
50
106
|
dir = Nrename::Directory.new test_dir
|
|
51
107
|
expect(dir).to have(:no).numbered_files
|
|
@@ -63,19 +119,6 @@ describe Nrename::Directory do
|
|
|
63
119
|
dir = Nrename::Directory.new test_dir
|
|
64
120
|
expect(dir).to have(numbered_files.length).numbered_files
|
|
65
121
|
end
|
|
66
|
-
|
|
67
|
-
it 'does not care about directories' do
|
|
68
|
-
files = %w[1.rb 2.rb 3.rb]
|
|
69
|
-
dirs = %w[1 2 03 40 55]
|
|
70
|
-
|
|
71
|
-
inside test_dir do
|
|
72
|
-
touch files
|
|
73
|
-
mkdir dirs
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
dir = Nrename::Directory.new test_dir
|
|
77
|
-
expect(dir).to have(files.length).numbered_files
|
|
78
|
-
end
|
|
79
122
|
end
|
|
80
123
|
|
|
81
124
|
describe '#field_length' do
|
|
@@ -132,6 +175,7 @@ describe Nrename::Directory do
|
|
|
132
175
|
dir.normalize
|
|
133
176
|
|
|
134
177
|
expected = %w[01 02 03 04 05 06 10 11]
|
|
178
|
+
|
|
135
179
|
renamed_files = Dir.entries(test_dir).
|
|
136
180
|
reject &(File.method :directory?)
|
|
137
181
|
|
|
@@ -20,6 +20,13 @@ describe Nrename::NumberedFile do
|
|
|
20
20
|
file = numbered_file '0001.txt'
|
|
21
21
|
expect(file.number).to be == 1
|
|
22
22
|
end
|
|
23
|
+
|
|
24
|
+
it 'handles different regular expressions' do
|
|
25
|
+
Nrename.options.stub :pattern => /^foo_0001_(\d+)\.txt$/
|
|
26
|
+
|
|
27
|
+
file = numbered_file 'foo_0001_0452.txt'
|
|
28
|
+
expect(file.number).to be == 452
|
|
29
|
+
end
|
|
23
30
|
end
|
|
24
31
|
|
|
25
32
|
describe '#normalized_path' do
|
|
@@ -31,11 +38,8 @@ describe Nrename::NumberedFile do
|
|
|
31
38
|
it 'normalizes file name' do
|
|
32
39
|
file = numbered_file 'abc_34.rb'
|
|
33
40
|
|
|
34
|
-
file.stub :number_length =>
|
|
35
|
-
expect(file.normalized_path.to_s).to be == '
|
|
36
|
-
|
|
37
|
-
file.stub :number_length => 5
|
|
38
|
-
expect(file.normalized_path.to_s).to be == '00034.rb'
|
|
41
|
+
file.stub :number_length => 3
|
|
42
|
+
expect(file.normalized_path.to_s).to be == '034.rb'
|
|
39
43
|
end
|
|
40
44
|
end
|
|
41
45
|
|
|
@@ -47,9 +51,6 @@ describe Nrename::NumberedFile do
|
|
|
47
51
|
it 'normalizes file name' do
|
|
48
52
|
file = numbered_file 'abc_34.rb'
|
|
49
53
|
|
|
50
|
-
file.stub :number_length => 4
|
|
51
|
-
expect(file.normalized_path.to_s).to be == 'abc_0034.rb'
|
|
52
|
-
|
|
53
54
|
file.stub :number_length => 5
|
|
54
55
|
expect(file.normalized_path.to_s).to be == 'abc_00034.rb'
|
|
55
56
|
end
|
|
@@ -62,9 +63,6 @@ describe Nrename::NumberedFile do
|
|
|
62
63
|
|
|
63
64
|
file.stub :number_length => 4
|
|
64
65
|
expect(file.adjusted_number).to be == '0123'
|
|
65
|
-
|
|
66
|
-
file.stub :number_length => 6
|
|
67
|
-
expect(file.adjusted_number).to be == '000123'
|
|
68
66
|
end
|
|
69
67
|
end
|
|
70
68
|
end
|
|
@@ -1,31 +1,115 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
|
+
require 'forwardable'
|
|
2
3
|
|
|
3
4
|
describe Nrename::Options do
|
|
5
|
+
extend Forwardable
|
|
6
|
+
|
|
7
|
+
def_delegator Nrename, :parse_options
|
|
8
|
+
|
|
9
|
+
after :each do
|
|
10
|
+
Nrename.options.reset # do not leak state
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
let(:defaults) { parse_options([]) }
|
|
14
|
+
|
|
4
15
|
describe 'verbosity' do
|
|
5
|
-
it 'is on by default
|
|
6
|
-
expect(
|
|
16
|
+
it 'is on by default' do
|
|
17
|
+
expect(defaults.verbose).to be_true
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it 'is on when "-v" switch is provided' do
|
|
21
|
+
expect(parse_options(%w[-v]).verbose).to be_true
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it 'is on when "--verbose" switch is provided' do
|
|
25
|
+
expect(parse_options(%w[--verbose]).verbose).to be_true
|
|
7
26
|
end
|
|
8
27
|
|
|
9
|
-
it 'is off when
|
|
10
|
-
expect(
|
|
28
|
+
it 'is off when "--no-verbose" switch is provided' do
|
|
29
|
+
expect(parse_options(%w[--no-verbose]).verbose).to be_false
|
|
11
30
|
end
|
|
12
31
|
end
|
|
13
32
|
|
|
14
33
|
describe 'recursive processing' do
|
|
15
34
|
it 'is off by default' do
|
|
16
|
-
expect(
|
|
35
|
+
expect(defaults.recursive).to be_false
|
|
17
36
|
end
|
|
18
37
|
|
|
19
|
-
it 'is on when "-R"
|
|
20
|
-
%w[-R
|
|
21
|
-
|
|
22
|
-
|
|
38
|
+
it 'is on when "-R" switch is provided' do
|
|
39
|
+
expect(parse_options(%w[-R]).recursive).to be_true
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
it 'is on when "--recursive" switch is provided' do
|
|
43
|
+
expect(parse_options(%w[--recursive]).recursive).to be_true
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
describe 'perform actual renaming' do
|
|
48
|
+
it 'is off by default' do
|
|
49
|
+
expect(defaults.execute).to be_false
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it 'is on when "-X" switch is provided' do
|
|
53
|
+
expect(parse_options(%w[-X]).execute).to be_true
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
it 'is on when "--execute" switch is provided' do
|
|
57
|
+
expect(parse_options(%w[--execute]).execute).to be_true
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
describe 'regular expression' do
|
|
62
|
+
it 'has a default value' do
|
|
63
|
+
expect(defaults.pattern).to be == /(\d+)/
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it 'can be provided with "--regexp" switch' do
|
|
67
|
+
regexp = parse_options(%w[--regexp _(\d+)_]).pattern
|
|
68
|
+
expect(regexp).to be == /_(\d+)_/
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
describe 'leaving numbers only' do
|
|
73
|
+
it 'is off by default' do
|
|
74
|
+
expect(defaults.numbers_only).to be_false
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
it 'is on when "-N" switch is provided' do
|
|
78
|
+
expect(parse_options(%w[-N]).numbers_only).to be_true
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
it 'is on when "--numbers-only" switch is provided' do
|
|
82
|
+
expect(parse_options(%w[--numbers-only]).numbers_only).to be_true
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
describe 'renaming of directories' do
|
|
87
|
+
it 'is off by default' do
|
|
88
|
+
expect(defaults.rename_dirs).to be_false
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
it 'is on when "-D" switch is provided' do
|
|
92
|
+
expect(parse_options(%w[-D]).rename_dirs).to be_true
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
it 'is on when "--rename-dirs" switch is provided' do
|
|
96
|
+
expect(parse_options(%w[--rename-dirs]).rename_dirs).to be_true
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
describe 'renumbering files from scratch' do
|
|
101
|
+
it 'is off by default' do
|
|
102
|
+
expect(defaults.renumber).to be_false
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
it 'is on when "--renumber" switch is provided' do
|
|
106
|
+
expect(parse_options(%w[--renumber]).renumber).to be_true
|
|
23
107
|
end
|
|
24
108
|
end
|
|
25
109
|
|
|
26
110
|
describe 'directory arguments' do
|
|
27
|
-
let(:test_dir) { File.expand_path '
|
|
28
|
-
let(:subdirs) {
|
|
111
|
+
let(:test_dir) { File.expand_path 'test ][ directory' }
|
|
112
|
+
let(:subdirs) { ['aa', 'bb', 'cc' 'foo bar [baz] (quux)'] }
|
|
29
113
|
|
|
30
114
|
before :all do
|
|
31
115
|
inside test_dir do
|
|
@@ -41,16 +125,16 @@ describe Nrename::Options do
|
|
|
41
125
|
subdirs.each do |subdir|
|
|
42
126
|
subdir_path = File.expand_path subdir, test_dir
|
|
43
127
|
arg = File.join test_dir, subdir
|
|
44
|
-
expect(
|
|
128
|
+
expect(parse_options([arg]).dirs).to include subdir_path
|
|
45
129
|
end
|
|
46
130
|
end
|
|
47
131
|
|
|
48
132
|
it 'empty if no arguments provided' do
|
|
49
|
-
expect(
|
|
133
|
+
expect(defaults.dirs).to be_empty
|
|
50
134
|
end
|
|
51
135
|
|
|
52
136
|
it 'recursively captures all subdirs when -R option provided' do
|
|
53
|
-
dirs =
|
|
137
|
+
dirs = parse_options(['-R', test_dir]).dirs
|
|
54
138
|
|
|
55
139
|
expect(dirs).to include test_dir
|
|
56
140
|
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: nrename
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.1
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -9,7 +9,7 @@ authors:
|
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date: 2012-
|
|
12
|
+
date: 2012-09-07 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: aruba
|
|
@@ -59,6 +59,22 @@ dependencies:
|
|
|
59
59
|
- - ~>
|
|
60
60
|
- !ruby/object:Gem::Version
|
|
61
61
|
version: 0.9.2
|
|
62
|
+
- !ruby/object:Gem::Dependency
|
|
63
|
+
name: rb-inotify
|
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
|
65
|
+
none: false
|
|
66
|
+
requirements:
|
|
67
|
+
- - ~>
|
|
68
|
+
- !ruby/object:Gem::Version
|
|
69
|
+
version: 0.8.8
|
|
70
|
+
type: :development
|
|
71
|
+
prerelease: false
|
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
73
|
+
none: false
|
|
74
|
+
requirements:
|
|
75
|
+
- - ~>
|
|
76
|
+
- !ruby/object:Gem::Version
|
|
77
|
+
version: 0.8.8
|
|
62
78
|
- !ruby/object:Gem::Dependency
|
|
63
79
|
name: rspec
|
|
64
80
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -115,7 +131,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
115
131
|
version: '0'
|
|
116
132
|
segments:
|
|
117
133
|
- 0
|
|
118
|
-
hash:
|
|
134
|
+
hash: 3302606396241923876
|
|
119
135
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
120
136
|
none: false
|
|
121
137
|
requirements:
|