tty-file 0.7.0 → 0.7.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 +4 -4
- data/CHANGELOG.md +10 -0
- data/lib/tty/file.rb +35 -21
- data/lib/tty/file/version.rb +1 -1
- data/spec/spec_helper.rb +4 -0
- data/spec/unit/append_to_file_spec.rb +96 -71
- data/spec/unit/binary_spec.rb +19 -1
- data/spec/unit/checksum_file_spec.rb +9 -0
- data/spec/unit/chmod_spec.rb +65 -51
- data/spec/unit/copy_directory_spec.rb +110 -96
- data/spec/unit/copy_file_spec.rb +117 -102
- data/spec/unit/create_directory_spec.rb +83 -69
- data/spec/unit/create_file_spec.rb +96 -82
- data/spec/unit/diff_spec.rb +97 -83
- data/spec/unit/download_file_spec.rb +49 -35
- data/spec/unit/inject_into_file_spec.rb +162 -148
- data/spec/unit/prepend_to_file_spec.rb +109 -83
- data/spec/unit/remove_file_spec.rb +45 -31
- data/spec/unit/replace_in_file_spec.rb +127 -113
- data/spec/unit/tail_file_spec.rb +61 -47
- data/tty-file.gemspec +4 -4
- metadata +12 -13
data/spec/unit/binary_spec.rb
CHANGED
@@ -24,18 +24,36 @@ RSpec.describe TTY::File, '#binary?' do
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
-
it "identifies image as binary" do
|
27
|
+
it "identifies image file as binary" do
|
28
28
|
file = tmp_path('blackhole.png')
|
29
29
|
|
30
30
|
expect(TTY::File.binary?(file)).to eq(true)
|
31
31
|
end
|
32
32
|
|
33
|
+
it "identifies an image file given as a pathname as binary" do
|
34
|
+
file = tmp_pathname('blackhole.png')
|
35
|
+
|
36
|
+
expect(TTY::File.binary?(file)).to eq(true)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'identifies image file provided by a Pathname instance as binary' do
|
40
|
+
file = tmp_pathname('blackhole.png')
|
41
|
+
|
42
|
+
expect(TTY::File.binary?(file)).to eq(true)
|
43
|
+
end
|
44
|
+
|
33
45
|
it "indetifies text file as non-binary" do
|
34
46
|
file = tmp_path('Gemfile')
|
35
47
|
|
36
48
|
expect(TTY::File.binary?(file)).to eq(false)
|
37
49
|
end
|
38
50
|
|
51
|
+
it 'identifies text file provided by a Pathname instance as non-binary' do
|
52
|
+
file = tmp_pathname('Gemfile')
|
53
|
+
|
54
|
+
expect(TTY::File.binary?(file)).to eq(false)
|
55
|
+
end
|
56
|
+
|
39
57
|
it "indetifies a null-terminated string file as binary" do
|
40
58
|
Tempfile.open('tty-file-binary-spec') do |file|
|
41
59
|
file.write("Binary content.\0")
|
@@ -10,6 +10,15 @@ RSpec.describe TTY::File, '#checksum_file' do
|
|
10
10
|
expect(checksum).to eq(expected)
|
11
11
|
end
|
12
12
|
|
13
|
+
it "generates checksum for a Pathname instance" do
|
14
|
+
file = tmp_pathname('checksum/README.md')
|
15
|
+
|
16
|
+
checksum = TTY::File.checksum_file(file)
|
17
|
+
expected = '76ba1beb6c611fa32624ed253444138cdf23eb938a3812137f8a399c5b375bfe'
|
18
|
+
|
19
|
+
expect(checksum).to eq(expected)
|
20
|
+
end
|
21
|
+
|
13
22
|
it "generates checksum for IO object" do
|
14
23
|
io = StringIO.new("Some content\nThe end")
|
15
24
|
|
data/spec/unit/chmod_spec.rb
CHANGED
@@ -1,78 +1,92 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
RSpec.describe TTY::File,
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
RSpec.describe TTY::File, "#chmod" do
|
4
|
+
shared_context "changing file permissions" do
|
5
|
+
context 'when octal permisssions' do
|
6
|
+
it "adds permissions to file - user executable",
|
7
|
+
unless: RSpec::Support::OS.windows? do
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
file = path_factory.call('script.sh')
|
10
|
+
mode = File.lstat(file).mode
|
11
|
+
expect(File.executable?(file)).to eq(false)
|
11
12
|
|
12
|
-
|
13
|
+
TTY::File.chmod(file, mode | TTY::File::U_X, verbose: false)
|
13
14
|
|
14
|
-
|
15
|
-
|
15
|
+
expect(File.lstat(file).mode).to eq(mode | TTY::File::U_X)
|
16
|
+
end
|
16
17
|
|
17
|
-
|
18
|
-
|
18
|
+
it "logs status when :verbose flag is true",
|
19
|
+
unless: RSpec::Support::OS.windows? do
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
|
21
|
+
file = path_factory.call('script.sh')
|
22
|
+
mode = File.lstat(file).mode
|
23
|
+
expect(File.executable?(file)).to eq(false)
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
25
|
+
expect {
|
26
|
+
TTY::File.chmod(file, mode | TTY::File::U_X)
|
27
|
+
}.to output(/chmod/).to_stdout_from_any_process
|
27
28
|
|
28
|
-
|
29
|
-
|
29
|
+
expect(File.lstat(file).mode).to eq(mode | TTY::File::U_X)
|
30
|
+
end
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
32
|
+
it "doesn't change permission when :noop flag is true" do
|
33
|
+
file = path_factory.call('script.sh')
|
34
|
+
mode = File.lstat(file).mode
|
35
|
+
expect(File.executable?(file)).to eq(false)
|
35
36
|
|
36
|
-
|
37
|
+
TTY::File.chmod(file, mode | TTY::File::U_X, verbose: false, noop: true)
|
37
38
|
|
38
|
-
|
39
|
+
expect(File.lstat(file).mode).to eq(mode)
|
40
|
+
end
|
39
41
|
end
|
40
|
-
end
|
41
42
|
|
42
|
-
|
43
|
-
|
44
|
-
|
43
|
+
context 'when human readable permissions' do
|
44
|
+
it "adds permisions to file - user executable",
|
45
|
+
unless: RSpec::Support::OS.windows? do
|
45
46
|
|
46
|
-
|
47
|
-
|
48
|
-
|
47
|
+
file = path_factory.call('script.sh')
|
48
|
+
mode = File.lstat(file).mode
|
49
|
+
expect(File.executable?(file)).to eq(false)
|
49
50
|
|
50
|
-
|
51
|
+
TTY::File.chmod(file, 'u+x', verbose: false)
|
51
52
|
|
52
|
-
|
53
|
-
|
53
|
+
expect(File.lstat(file).mode).to eq(mode | TTY::File::U_X)
|
54
|
+
end
|
54
55
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
56
|
+
it "removes permission for user executable" do
|
57
|
+
file = path_factory.call('script.sh')
|
58
|
+
mode = File.lstat(file).mode
|
59
|
+
expect(File.writable?(file)).to eq(true)
|
59
60
|
|
60
|
-
|
61
|
+
TTY::File.chmod(file, 'u-w', verbose: false)
|
61
62
|
|
62
|
-
|
63
|
-
|
64
|
-
|
63
|
+
expect(File.lstat(file).mode).to eq(mode ^ TTY::File::U_W)
|
64
|
+
expect(File.writable?(file)).to eq(false)
|
65
|
+
end
|
65
66
|
|
66
|
-
|
67
|
-
|
67
|
+
it "adds multiple permissions separated by comma",
|
68
|
+
unless: RSpec::Support::OS.windows? do
|
68
69
|
|
69
|
-
|
70
|
-
|
71
|
-
|
70
|
+
file = path_factory.call('script.sh')
|
71
|
+
mode = File.lstat(file).mode
|
72
|
+
expect(File.executable?(file)).to eq(false)
|
72
73
|
|
73
|
-
|
74
|
+
TTY::File.chmod(file, 'u+x,g+x', verbose: false)
|
74
75
|
|
75
|
-
|
76
|
+
expect(File.lstat(file).mode).to eq(mode | TTY::File::U_X | TTY::File::G_X)
|
77
|
+
end
|
76
78
|
end
|
77
79
|
end
|
80
|
+
|
81
|
+
context "when passed a String instance for the file argument" do
|
82
|
+
let(:path_factory) { method(:tmp_path) }
|
83
|
+
|
84
|
+
include_context "changing file permissions"
|
85
|
+
end
|
86
|
+
|
87
|
+
context "when passed a Pathname instance for the file argument" do
|
88
|
+
let(:path_factory) { method(:tmp_pathname) }
|
89
|
+
|
90
|
+
include_context "changing file permissions"
|
91
|
+
end
|
78
92
|
end
|
@@ -1,106 +1,120 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
RSpec.describe TTY::File,
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
3
|
+
RSpec.describe TTY::File, "#copy_directory" do
|
4
|
+
shared_context "copies directory" do
|
5
|
+
it "copies directory of files recursively" do
|
6
|
+
app = path_factory.call('cli_app')
|
7
|
+
apps = path_factory.call('apps')
|
8
|
+
|
9
|
+
variables = OpenStruct.new
|
10
|
+
variables[:name] = 'tty'
|
11
|
+
variables[:foo] = 'Foo'
|
12
|
+
variables[:bar] = 'Bar'
|
13
|
+
|
14
|
+
TTY::File.copy_directory(app, apps, context: variables, verbose: false)
|
15
|
+
|
16
|
+
expect(Find.find(apps.to_s).to_a).to eq([
|
17
|
+
tmp_path('apps'),
|
18
|
+
tmp_path('apps/README'),
|
19
|
+
tmp_path('apps/command.rb'),
|
20
|
+
tmp_path('apps/commands'),
|
21
|
+
tmp_path('apps/commands/subcommand.rb'),
|
22
|
+
tmp_path('apps/excluded'),
|
23
|
+
tmp_path('apps/excluded/command.rb'),
|
24
|
+
tmp_path('apps/excluded/tty_cli.rb'),
|
25
|
+
tmp_path('apps/tty_cli.rb')
|
26
|
+
])
|
27
|
+
|
28
|
+
expect(File.read(tmp_path('apps/command.rb'))).to eq("class FooCommand\nend\n")
|
29
|
+
expect(File.read(tmp_path('apps/excluded/command.rb'))).to eq("class BarCommand\nend\n")
|
30
|
+
end
|
31
|
+
|
32
|
+
it "copies top level directory of files and evalutes templates" do
|
33
|
+
app = path_factory.call('cli_app')
|
34
|
+
apps = path_factory.call('apps')
|
35
|
+
|
36
|
+
variables = OpenStruct.new
|
37
|
+
variables[:name] = 'tty'
|
38
|
+
variables[:foo] = 'Foo'
|
39
|
+
variables[:bar] = 'Bar'
|
40
|
+
|
41
|
+
TTY::File.copy_directory(app, apps, recursive: false,
|
42
|
+
context: variables,
|
43
|
+
verbose: false)
|
44
|
+
|
45
|
+
expect(Find.find(apps.to_s).to_a).to eq([
|
46
|
+
tmp_path('apps'),
|
47
|
+
tmp_path('apps/README'),
|
48
|
+
tmp_path('apps/command.rb'),
|
49
|
+
tmp_path('apps/tty_cli.rb')
|
50
|
+
])
|
51
|
+
end
|
52
|
+
|
53
|
+
it "handles glob characters in the path" do
|
54
|
+
src = path_factory.call("foo[1]")
|
55
|
+
dest = path_factory.call("foo1")
|
56
|
+
TTY::File.copy_directory(src, dest, verbose: false)
|
57
|
+
|
58
|
+
expect(Find.find(dest.to_s).to_a).to eq([
|
59
|
+
tmp_path('foo1'),
|
60
|
+
tmp_path('foo1/README.md')
|
61
|
+
])
|
62
|
+
end
|
63
|
+
|
64
|
+
it "ignores excluded directories" do
|
65
|
+
src = path_factory.call('cli_app')
|
66
|
+
dest = path_factory.call('ignored')
|
67
|
+
|
68
|
+
variables = OpenStruct.new
|
69
|
+
variables[:name] = 'tty'
|
70
|
+
variables[:foo] = 'Foo'
|
71
|
+
variables[:bar] = 'Bar'
|
72
|
+
|
73
|
+
TTY::File.copy_directory(src, dest, context: variables,
|
74
|
+
exclude: %r{excluded/},
|
75
|
+
verbose: false)
|
76
|
+
|
77
|
+
expect(Find.find(dest.to_s).to_a).to eq([
|
78
|
+
tmp_path('ignored'),
|
79
|
+
tmp_path('ignored/README'),
|
80
|
+
tmp_path('ignored/command.rb'),
|
81
|
+
tmp_path('ignored/commands'),
|
82
|
+
tmp_path('ignored/commands/subcommand.rb'),
|
83
|
+
tmp_path('ignored/tty_cli.rb')
|
84
|
+
])
|
85
|
+
end
|
86
|
+
|
87
|
+
it "raises error when source directory doesn't exist" do
|
88
|
+
expect {
|
89
|
+
TTY::File.copy_directory('unknown')
|
90
|
+
}.to raise_error(ArgumentError, %r{File path "unknown" does not exist.})
|
91
|
+
end
|
92
|
+
|
93
|
+
it "logs status" do
|
94
|
+
app = path_factory.call('cli_app')
|
95
|
+
apps = path_factory.call('apps')
|
96
|
+
|
97
|
+
variables = OpenStruct.new
|
98
|
+
variables[:name] = 'tty'
|
99
|
+
variables[:class_name] = 'TTY'
|
100
|
+
|
101
|
+
expect {
|
102
|
+
TTY::File.copy_directory(app, apps, context: variables, verbose: true)
|
103
|
+
}.to output(
|
104
|
+
%r{create(.*)apps/tty_cli.rb\n(.*)create(.*)apps/README\n(.*)create(.*)apps/command.rb\n}m
|
105
|
+
).to_stdout_from_any_process
|
106
|
+
end
|
29
107
|
end
|
30
108
|
|
31
|
-
|
32
|
-
|
33
|
-
apps = tmp_path('apps')
|
34
|
-
|
35
|
-
variables = OpenStruct.new
|
36
|
-
variables[:name] = 'tty'
|
37
|
-
variables[:foo] = 'Foo'
|
38
|
-
variables[:bar] = 'Bar'
|
39
|
-
|
40
|
-
TTY::File.copy_directory(app, apps, recursive: false,
|
41
|
-
context: variables,
|
42
|
-
verbose: false)
|
43
|
-
|
44
|
-
expect(Find.find(apps).to_a).to eq([
|
45
|
-
tmp_path('apps'),
|
46
|
-
tmp_path('apps/README'),
|
47
|
-
tmp_path('apps/command.rb'),
|
48
|
-
tmp_path('apps/tty_cli.rb')
|
49
|
-
])
|
50
|
-
end
|
51
|
-
|
52
|
-
it "handles glob characters in the path" do
|
53
|
-
src = tmp_path("foo[1]")
|
54
|
-
dest = tmp_path("foo1")
|
55
|
-
TTY::File.copy_directory(src, dest, verbose: false)
|
56
|
-
|
57
|
-
expect(Find.find(dest).to_a).to eq([
|
58
|
-
tmp_path('foo1'),
|
59
|
-
tmp_path('foo1/README.md')
|
60
|
-
])
|
61
|
-
end
|
109
|
+
context "when passed String instances for the file arguments" do
|
110
|
+
let(:path_factory) { method(:tmp_path) }
|
62
111
|
|
63
|
-
|
64
|
-
src = tmp_path('cli_app')
|
65
|
-
dest = tmp_path('ignored')
|
66
|
-
|
67
|
-
variables = OpenStruct.new
|
68
|
-
variables[:name] = 'tty'
|
69
|
-
variables[:foo] = 'Foo'
|
70
|
-
variables[:bar] = 'Bar'
|
71
|
-
|
72
|
-
TTY::File.copy_directory(src, dest, context: variables,
|
73
|
-
exclude: %r{excluded/},
|
74
|
-
verbose: false)
|
75
|
-
|
76
|
-
expect(Find.find(dest).to_a).to eq([
|
77
|
-
tmp_path('ignored'),
|
78
|
-
tmp_path('ignored/README'),
|
79
|
-
tmp_path('ignored/command.rb'),
|
80
|
-
tmp_path('ignored/commands'),
|
81
|
-
tmp_path('ignored/commands/subcommand.rb'),
|
82
|
-
tmp_path('ignored/tty_cli.rb')
|
83
|
-
])
|
112
|
+
it_behaves_like "copies directory"
|
84
113
|
end
|
85
114
|
|
86
|
-
|
87
|
-
|
88
|
-
TTY::File.copy_directory('unknown')
|
89
|
-
}.to raise_error(ArgumentError, %r{File path "unknown" does not exist.})
|
90
|
-
end
|
91
|
-
|
92
|
-
it "logs status" do
|
93
|
-
app = tmp_path('cli_app')
|
94
|
-
apps = tmp_path('apps')
|
95
|
-
|
96
|
-
variables = OpenStruct.new
|
97
|
-
variables[:name] = 'tty'
|
98
|
-
variables[:class_name] = 'TTY'
|
115
|
+
context "when passed Pathname instances for the file arguments" do
|
116
|
+
let(:path_factory) { method(:tmp_pathname) }
|
99
117
|
|
100
|
-
|
101
|
-
TTY::File.copy_directory(app, apps, context: variables, verbose: true)
|
102
|
-
}.to output(
|
103
|
-
%r{create(.*)apps/tty_cli.rb\n(.*)create(.*)apps/README\n(.*)create(.*)apps/command.rb\n}m
|
104
|
-
).to_stdout_from_any_process
|
118
|
+
it_behaves_like "copies directory"
|
105
119
|
end
|
106
120
|
end
|
data/spec/unit/copy_file_spec.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require 'ostruct'
|
3
4
|
|
4
|
-
RSpec.describe TTY::File,
|
5
|
+
RSpec.describe TTY::File, "#copy_file" do
|
5
6
|
def exists_and_identical?(source, dest)
|
6
7
|
dest_path = File.join(tmp_path, dest)
|
7
8
|
expect(::File.exist?(dest_path)).to be(true)
|
@@ -10,148 +11,162 @@ RSpec.describe TTY::File, '#copy_file' do
|
|
10
11
|
expect(::FileUtils).to be_identical(source_path, dest_path)
|
11
12
|
end
|
12
13
|
|
13
|
-
|
14
|
-
|
14
|
+
shared_context 'copying files' do
|
15
|
+
it "copies file without destination" do
|
16
|
+
src = path_factory.call('Gemfile')
|
15
17
|
|
16
|
-
|
18
|
+
TTY::File.copy_file(src, verbose: false)
|
17
19
|
|
18
|
-
|
19
|
-
|
20
|
+
exists_and_identical?('Gemfile', 'Gemfile')
|
21
|
+
end
|
20
22
|
|
21
|
-
|
22
|
-
|
23
|
-
|
23
|
+
it "copies file to the destination" do
|
24
|
+
src = path_factory.call('Gemfile')
|
25
|
+
dest = path_factory.call('app/Makefile')
|
24
26
|
|
25
|
-
|
27
|
+
TTY::File.copy_file(src, dest, verbose: false)
|
26
28
|
|
27
|
-
|
28
|
-
|
29
|
+
exists_and_identical?('Gemfile', 'app/Makefile')
|
30
|
+
end
|
29
31
|
|
30
|
-
|
31
|
-
|
32
|
-
|
32
|
+
it "copies file to existing destination" do
|
33
|
+
src = path_factory.call('Gemfile')
|
34
|
+
dest = path_factory.call('app/Gemfile')
|
33
35
|
|
34
|
-
|
36
|
+
TTY::File.copy_file(src, dest, verbose: false)
|
35
37
|
|
36
|
-
|
37
|
-
|
38
|
+
exists_and_identical?('Gemfile', 'app/Gemfile')
|
39
|
+
end
|
38
40
|
|
39
|
-
|
40
|
-
|
41
|
-
|
41
|
+
it "copies file with block content" do
|
42
|
+
src = path_factory.call('Gemfile')
|
43
|
+
dest = path_factory.call('app/Gemfile')
|
42
44
|
|
43
|
-
|
44
|
-
|
45
|
+
TTY::File.copy_file(src, dest, verbose: false) do |content|
|
46
|
+
"https://rubygems.org\n" + content
|
47
|
+
end
|
48
|
+
expect(File.read(dest)).to eq("https://rubygems.org\ngem 'nokogiri'\ngem 'rails', '5.0.0'\ngem 'rack', '>=1.0'\n")
|
45
49
|
end
|
46
|
-
expect(File.read(dest)).to eq("https://rubygems.org\ngem 'nokogiri'\ngem 'rails', '5.0.0'\ngem 'rack', '>=1.0'\n")
|
47
|
-
end
|
48
50
|
|
49
|
-
|
50
|
-
|
51
|
-
|
51
|
+
it "copies file and preservs metadata" do
|
52
|
+
src = path_factory.call('Gemfile')
|
53
|
+
dest = path_factory.call('app/Gemfile')
|
52
54
|
|
53
|
-
|
54
|
-
|
55
|
-
|
55
|
+
expect {
|
56
|
+
TTY::File.copy_file(src, dest, verbose: false, preserve: true)
|
57
|
+
}.to output('').to_stdout_from_any_process
|
56
58
|
|
57
|
-
|
58
|
-
|
59
|
+
expect(File.stat(src)).to eq(File.stat(dest))
|
60
|
+
end
|
59
61
|
|
60
|
-
|
61
|
-
|
62
|
-
|
62
|
+
it "doesn't copy file if :noop is true" do
|
63
|
+
src = path_factory.call('Gemfile')
|
64
|
+
dest = path_factory.call('app/Gemfile')
|
63
65
|
|
64
|
-
|
66
|
+
TTY::File.copy_file(src, dest, verbose: false, noop: true)
|
65
67
|
|
66
|
-
|
67
|
-
|
68
|
+
expect(File.exist?(dest)).to eq(false)
|
69
|
+
end
|
68
70
|
|
69
|
-
|
70
|
-
|
71
|
-
|
71
|
+
it "logs status" do
|
72
|
+
src = path_factory.call('Gemfile')
|
73
|
+
dest = path_factory.call('app/Gemfile')
|
72
74
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
75
|
+
expect {
|
76
|
+
TTY::File.copy_file(src, dest)
|
77
|
+
}.to output(/\e\[32mcreate\e\[0m.*Gemfile/).to_stdout_from_any_process
|
78
|
+
end
|
77
79
|
|
78
|
-
|
79
|
-
|
80
|
-
|
80
|
+
it "logs status without color" do
|
81
|
+
src = path_factory.call('Gemfile')
|
82
|
+
dest = path_factory.call('app/Gemfile')
|
81
83
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
84
|
+
expect {
|
85
|
+
TTY::File.copy_file(src, dest, color: false)
|
86
|
+
}.to output(/\s+create.*Gemfile/).to_stdout_from_any_process
|
87
|
+
end
|
86
88
|
|
87
|
-
|
88
|
-
|
89
|
+
it "removes template .erb extension" do
|
90
|
+
src = path_factory.call('templates/application.html.erb')
|
89
91
|
|
90
|
-
|
92
|
+
TTY::File.copy_file(src, verbose: false)
|
91
93
|
|
92
|
-
|
93
|
-
|
94
|
-
|
94
|
+
exists_and_identical?('templates/application.html.erb',
|
95
|
+
'templates/application.html')
|
96
|
+
end
|
95
97
|
|
96
|
-
|
97
|
-
|
98
|
-
|
98
|
+
it "converts filename based on context" do
|
99
|
+
src = path_factory.call('templates/%file_name%.rb')
|
100
|
+
dest = path_factory.call('app/%file_name%.rb')
|
99
101
|
|
100
|
-
|
101
|
-
|
102
|
-
|
102
|
+
variables = OpenStruct.new
|
103
|
+
variables[:foo] = 'bar'
|
104
|
+
variables[:file_name] = 'expected'
|
103
105
|
|
104
|
-
|
106
|
+
TTY::File.copy_file(src, dest, context: variables, verbose: false)
|
105
107
|
|
106
|
-
|
107
|
-
|
108
|
-
|
108
|
+
expected = tmp_path('app/expected.rb')
|
109
|
+
expect(File.read(expected)).to eq("bar\n")
|
110
|
+
end
|
109
111
|
|
110
|
-
|
111
|
-
|
112
|
-
|
112
|
+
it "converts filename based on class context" do
|
113
|
+
src = path_factory.call('templates/%file_name%.rb')
|
114
|
+
dest = path_factory.call('templates/expected.rb')
|
113
115
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
116
|
+
stub_const('TestCase', Class.new {
|
117
|
+
def foo
|
118
|
+
'bar'
|
119
|
+
end
|
118
120
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
121
|
+
def file_name
|
122
|
+
'expected'
|
123
|
+
end
|
124
|
+
})
|
125
|
+
TestCase.send(:include, TTY::File)
|
124
126
|
|
125
|
-
|
127
|
+
TestCase.new.send(:copy_file, src, verbose: false)
|
126
128
|
|
127
|
-
|
128
|
-
|
129
|
+
expect(File.read(dest)).to eq("bar\n")
|
130
|
+
end
|
129
131
|
|
130
|
-
|
131
|
-
|
132
|
-
|
132
|
+
it "copies file with custom class context" do
|
133
|
+
src = path_factory.call('templates/unit_test.rb')
|
134
|
+
dest = path_factory.call('test/unit_test.rb')
|
133
135
|
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
136
|
+
stub_const('TestCase', Class.new {
|
137
|
+
def self.class_name
|
138
|
+
'Example'
|
139
|
+
end
|
140
|
+
})
|
141
|
+
TestCase.extend(TTY::File)
|
142
|
+
|
143
|
+
TestCase.send(:copy_file, src, dest, verbose: false)
|
144
|
+
|
145
|
+
expect(File.read(dest)).to eq("class ExampleTest; end\n")
|
146
|
+
end
|
140
147
|
|
141
|
-
|
148
|
+
it "copies template with custom context binding" do
|
149
|
+
src = path_factory.call('templates/unit_test.rb')
|
150
|
+
dest = path_factory.call('test/unit_test.rb')
|
142
151
|
|
143
|
-
|
152
|
+
variables = OpenStruct.new
|
153
|
+
variables[:class_name] = 'Example'
|
154
|
+
|
155
|
+
TTY::File.copy_file(src, dest, context: variables, verbose: false)
|
156
|
+
|
157
|
+
expect(File.read(dest)).to eq("class ExampleTest; end\n")
|
158
|
+
end
|
144
159
|
end
|
145
160
|
|
146
|
-
|
147
|
-
|
148
|
-
dest = tmp_path('test/unit_test.rb')
|
161
|
+
context "when passed String instances for the file arguments" do
|
162
|
+
let(:path_factory) { method(:tmp_path) }
|
149
163
|
|
150
|
-
|
151
|
-
|
164
|
+
include_context "copying files"
|
165
|
+
end
|
152
166
|
|
153
|
-
|
167
|
+
context "when passed Pathname instances for the file arguments" do
|
168
|
+
let(:path_factory) { method(:tmp_pathname) }
|
154
169
|
|
155
|
-
|
170
|
+
include_context "copying files"
|
156
171
|
end
|
157
172
|
end
|