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.
@@ -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
 
@@ -1,78 +1,92 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- RSpec.describe TTY::File, '#chmod' do
4
- context 'when octal permisssions' do
5
- it "adds permissions to file - user executable",
6
- unless: RSpec::Support::OS.windows? do
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
- file = tmp_path('script.sh')
9
- mode = File.lstat(file).mode
10
- expect(File.executable?(file)).to eq(false)
9
+ file = path_factory.call('script.sh')
10
+ mode = File.lstat(file).mode
11
+ expect(File.executable?(file)).to eq(false)
11
12
 
12
- TTY::File.chmod(file, mode | TTY::File::U_X, verbose: false)
13
+ TTY::File.chmod(file, mode | TTY::File::U_X, verbose: false)
13
14
 
14
- expect(File.lstat(file).mode).to eq(mode | TTY::File::U_X)
15
- end
15
+ expect(File.lstat(file).mode).to eq(mode | TTY::File::U_X)
16
+ end
16
17
 
17
- it "logs status when :verbose flag is true",
18
- unless: RSpec::Support::OS.windows? do
18
+ it "logs status when :verbose flag is true",
19
+ unless: RSpec::Support::OS.windows? do
19
20
 
20
- file = tmp_path('script.sh')
21
- mode = File.lstat(file).mode
22
- expect(File.executable?(file)).to eq(false)
21
+ file = path_factory.call('script.sh')
22
+ mode = File.lstat(file).mode
23
+ expect(File.executable?(file)).to eq(false)
23
24
 
24
- expect {
25
- TTY::File.chmod(file, mode | TTY::File::U_X)
26
- }.to output(/chmod/).to_stdout_from_any_process
25
+ expect {
26
+ TTY::File.chmod(file, mode | TTY::File::U_X)
27
+ }.to output(/chmod/).to_stdout_from_any_process
27
28
 
28
- expect(File.lstat(file).mode).to eq(mode | TTY::File::U_X)
29
- end
29
+ expect(File.lstat(file).mode).to eq(mode | TTY::File::U_X)
30
+ end
30
31
 
31
- it "doesn't change permission when :noop flag is true" do
32
- file = tmp_path('script.sh')
33
- mode = File.lstat(file).mode
34
- expect(File.executable?(file)).to eq(false)
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
- TTY::File.chmod(file, mode | TTY::File::U_X, verbose: false, noop: true)
37
+ TTY::File.chmod(file, mode | TTY::File::U_X, verbose: false, noop: true)
37
38
 
38
- expect(File.lstat(file).mode).to eq(mode)
39
+ expect(File.lstat(file).mode).to eq(mode)
40
+ end
39
41
  end
40
- end
41
42
 
42
- context 'when human readable permissions' do
43
- it "adds permisions to file - user executable",
44
- unless: RSpec::Support::OS.windows? do
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
- file = tmp_path('script.sh')
47
- mode = File.lstat(file).mode
48
- expect(File.executable?(file)).to eq(false)
47
+ file = path_factory.call('script.sh')
48
+ mode = File.lstat(file).mode
49
+ expect(File.executable?(file)).to eq(false)
49
50
 
50
- TTY::File.chmod(file, 'u+x', verbose: false)
51
+ TTY::File.chmod(file, 'u+x', verbose: false)
51
52
 
52
- expect(File.lstat(file).mode).to eq(mode | TTY::File::U_X)
53
- end
53
+ expect(File.lstat(file).mode).to eq(mode | TTY::File::U_X)
54
+ end
54
55
 
55
- it "removes permission for user executable" do
56
- file = tmp_path('script.sh')
57
- mode = File.lstat(file).mode
58
- expect(File.writable?(file)).to eq(true)
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
- TTY::File.chmod(file, 'u-w', verbose: false)
61
+ TTY::File.chmod(file, 'u-w', verbose: false)
61
62
 
62
- expect(File.lstat(file).mode).to eq(mode ^ TTY::File::U_W)
63
- expect(File.writable?(file)).to eq(false)
64
- end
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
- it "adds multiple permissions separated by comma",
67
- unless: RSpec::Support::OS.windows? do
67
+ it "adds multiple permissions separated by comma",
68
+ unless: RSpec::Support::OS.windows? do
68
69
 
69
- file = tmp_path('script.sh')
70
- mode = File.lstat(file).mode
71
- expect(File.executable?(file)).to eq(false)
70
+ file = path_factory.call('script.sh')
71
+ mode = File.lstat(file).mode
72
+ expect(File.executable?(file)).to eq(false)
72
73
 
73
- TTY::File.chmod(file, 'u+x,g+x', verbose: false)
74
+ TTY::File.chmod(file, 'u+x,g+x', verbose: false)
74
75
 
75
- expect(File.lstat(file).mode).to eq(mode | TTY::File::U_X | TTY::File::G_X)
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, '#copy_directory' do
4
- it "copies directory of files recursively" do
5
- app = tmp_path('cli_app')
6
- apps = tmp_path('apps')
7
-
8
- variables = OpenStruct.new
9
- variables[:name] = 'tty'
10
- variables[:foo] = 'Foo'
11
- variables[:bar] = 'Bar'
12
-
13
- TTY::File.copy_directory(app, apps, context: variables, verbose: false)
14
-
15
- expect(Find.find(apps).to_a).to eq([
16
- tmp_path('apps'),
17
- tmp_path('apps/README'),
18
- tmp_path('apps/command.rb'),
19
- tmp_path('apps/commands'),
20
- tmp_path('apps/commands/subcommand.rb'),
21
- tmp_path('apps/excluded'),
22
- tmp_path('apps/excluded/command.rb'),
23
- tmp_path('apps/excluded/tty_cli.rb'),
24
- tmp_path('apps/tty_cli.rb')
25
- ])
26
-
27
- expect(File.read(tmp_path('apps/command.rb'))).to eq("class FooCommand\nend\n")
28
- expect(File.read(tmp_path('apps/excluded/command.rb'))).to eq("class BarCommand\nend\n")
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
- it "copies top level directory of files and evalutes templates" do
32
- app = tmp_path('cli_app')
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
- it "ignores excluded directories" do
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
- it "raises error when source directory doesn't exist" do
87
- expect {
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
- expect {
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
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'ostruct'
3
4
 
4
- RSpec.describe TTY::File, '#copy_file' do
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
- it "copies file without destination" do
14
- src = tmp_path('Gemfile')
14
+ shared_context 'copying files' do
15
+ it "copies file without destination" do
16
+ src = path_factory.call('Gemfile')
15
17
 
16
- TTY::File.copy_file(src, verbose: false)
18
+ TTY::File.copy_file(src, verbose: false)
17
19
 
18
- exists_and_identical?('Gemfile', 'Gemfile')
19
- end
20
+ exists_and_identical?('Gemfile', 'Gemfile')
21
+ end
20
22
 
21
- it "copies file to the destination" do
22
- src = tmp_path('Gemfile')
23
- dest = tmp_path('app/Makefile')
23
+ it "copies file to the destination" do
24
+ src = path_factory.call('Gemfile')
25
+ dest = path_factory.call('app/Makefile')
24
26
 
25
- TTY::File.copy_file(src, dest, verbose: false)
27
+ TTY::File.copy_file(src, dest, verbose: false)
26
28
 
27
- exists_and_identical?('Gemfile', 'app/Makefile')
28
- end
29
+ exists_and_identical?('Gemfile', 'app/Makefile')
30
+ end
29
31
 
30
- it "copies file to existing destination" do
31
- src = tmp_path('Gemfile')
32
- dest = tmp_path('app/Gemfile')
32
+ it "copies file to existing destination" do
33
+ src = path_factory.call('Gemfile')
34
+ dest = path_factory.call('app/Gemfile')
33
35
 
34
- TTY::File.copy_file(src, dest, verbose: false)
36
+ TTY::File.copy_file(src, dest, verbose: false)
35
37
 
36
- exists_and_identical?('Gemfile', 'app/Gemfile')
37
- end
38
+ exists_and_identical?('Gemfile', 'app/Gemfile')
39
+ end
38
40
 
39
- it "copies file with block content" do
40
- src = tmp_path('Gemfile')
41
- dest = tmp_path('app/Gemfile')
41
+ it "copies file with block content" do
42
+ src = path_factory.call('Gemfile')
43
+ dest = path_factory.call('app/Gemfile')
42
44
 
43
- TTY::File.copy_file(src, dest, verbose: false) do |content|
44
- "https://rubygems.org\n" + content
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
- it "copies file and preservs metadata" do
50
- src = tmp_path('Gemfile')
51
- dest = tmp_path('app/Gemfile')
51
+ it "copies file and preservs metadata" do
52
+ src = path_factory.call('Gemfile')
53
+ dest = path_factory.call('app/Gemfile')
52
54
 
53
- expect {
54
- TTY::File.copy_file(src, dest, verbose: false, preserve: true)
55
- }.to output('').to_stdout_from_any_process
55
+ expect {
56
+ TTY::File.copy_file(src, dest, verbose: false, preserve: true)
57
+ }.to output('').to_stdout_from_any_process
56
58
 
57
- expect(File.stat(src)).to eq(File.stat(dest))
58
- end
59
+ expect(File.stat(src)).to eq(File.stat(dest))
60
+ end
59
61
 
60
- it "doesn't copy file if :noop is true" do
61
- src = tmp_path('Gemfile')
62
- dest = tmp_path('app/Gemfile')
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
- TTY::File.copy_file(src, dest, verbose: false, noop: true)
66
+ TTY::File.copy_file(src, dest, verbose: false, noop: true)
65
67
 
66
- expect(File.exist?(dest)).to eq(false)
67
- end
68
+ expect(File.exist?(dest)).to eq(false)
69
+ end
68
70
 
69
- it "logs status" do
70
- src = tmp_path('Gemfile')
71
- dest = tmp_path('app/Gemfile')
71
+ it "logs status" do
72
+ src = path_factory.call('Gemfile')
73
+ dest = path_factory.call('app/Gemfile')
72
74
 
73
- expect {
74
- TTY::File.copy_file(src, dest)
75
- }.to output(/\e\[32mcreate\e\[0m.*Gemfile/).to_stdout_from_any_process
76
- end
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
- it "logs status without color" do
79
- src = tmp_path('Gemfile')
80
- dest = tmp_path('app/Gemfile')
80
+ it "logs status without color" do
81
+ src = path_factory.call('Gemfile')
82
+ dest = path_factory.call('app/Gemfile')
81
83
 
82
- expect {
83
- TTY::File.copy_file(src, dest, color: false)
84
- }.to output(/\s+create.*Gemfile/).to_stdout_from_any_process
85
- end
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
- it "removes template .erb extension" do
88
- src = tmp_path('templates/application.html.erb')
89
+ it "removes template .erb extension" do
90
+ src = path_factory.call('templates/application.html.erb')
89
91
 
90
- TTY::File.copy_file(src, verbose: false)
92
+ TTY::File.copy_file(src, verbose: false)
91
93
 
92
- exists_and_identical?('templates/application.html.erb',
93
- 'templates/application.html')
94
- end
94
+ exists_and_identical?('templates/application.html.erb',
95
+ 'templates/application.html')
96
+ end
95
97
 
96
- it "converts filename based on context" do
97
- src = tmp_path('templates/%file_name%.rb')
98
- dest = tmp_path('app/%file_name%.rb')
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
- variables = OpenStruct.new
101
- variables[:foo] = 'bar'
102
- variables[:file_name] = 'expected'
102
+ variables = OpenStruct.new
103
+ variables[:foo] = 'bar'
104
+ variables[:file_name] = 'expected'
103
105
 
104
- TTY::File.copy_file(src, dest, context: variables, verbose: false)
106
+ TTY::File.copy_file(src, dest, context: variables, verbose: false)
105
107
 
106
- expected = tmp_path('app/expected.rb')
107
- expect(File.read(expected)).to eq("bar\n")
108
- end
108
+ expected = tmp_path('app/expected.rb')
109
+ expect(File.read(expected)).to eq("bar\n")
110
+ end
109
111
 
110
- it "converts filename based on class context" do
111
- src = tmp_path('templates/%file_name%.rb')
112
- dest = tmp_path('templates/expected.rb')
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
- stub_const('TestCase', Class.new {
115
- def foo
116
- 'bar'
117
- end
116
+ stub_const('TestCase', Class.new {
117
+ def foo
118
+ 'bar'
119
+ end
118
120
 
119
- def file_name
120
- 'expected'
121
- end
122
- })
123
- TestCase.send(:include, TTY::File)
121
+ def file_name
122
+ 'expected'
123
+ end
124
+ })
125
+ TestCase.send(:include, TTY::File)
124
126
 
125
- TestCase.new.send(:copy_file, src, verbose: false)
127
+ TestCase.new.send(:copy_file, src, verbose: false)
126
128
 
127
- expect(File.read(dest)).to eq("bar\n")
128
- end
129
+ expect(File.read(dest)).to eq("bar\n")
130
+ end
129
131
 
130
- it "copies file with custom class context" do
131
- src = tmp_path('templates/unit_test.rb')
132
- dest = tmp_path('test/unit_test.rb')
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
- stub_const('TestCase', Class.new {
135
- def self.class_name
136
- 'Example'
137
- end
138
- })
139
- TestCase.extend(TTY::File)
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
- TestCase.send(:copy_file, src, dest, verbose: false)
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
- expect(File.read(dest)).to eq("class ExampleTest; end\n")
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
- it "copies template with custom context binding" do
147
- src = tmp_path('templates/unit_test.rb')
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
- variables = OpenStruct.new
151
- variables[:class_name] = 'Example'
164
+ include_context "copying files"
165
+ end
152
166
 
153
- TTY::File.copy_file(src, dest, context: variables, verbose: false)
167
+ context "when passed Pathname instances for the file arguments" do
168
+ let(:path_factory) { method(:tmp_pathname) }
154
169
 
155
- expect(File.read(dest)).to eq("class ExampleTest; end\n")
170
+ include_context "copying files"
156
171
  end
157
172
  end