winrm-fs 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,49 +1,49 @@
1
- $hash_file = <%= hash_file %>
2
-
3
- Function Cleanup($disposable) {
4
- if (($disposable -ne $null) -and ($disposable.GetType().GetMethod("Dispose") -ne $null)) {
5
- $disposable.Dispose()
6
- }
7
- }
8
-
9
- Function Check-Files($h) {
10
- return $h.GetEnumerator() | ForEach-Object {
11
- $dst = Unresolve-Path $_.Value.target
12
- $dst_changed = $false
13
- if(Test-Path $dst -PathType Container) {
14
- $dst_changed = $true
15
- $dst = Join-Path $dst $_.Value.src_basename
16
- }
17
- New-Object psobject -Property @{
18
- chk_exists = ($exists = Test-Path $dst -PathType Leaf)
19
- src_md5 = ($sMd5 = $_.Key)
20
- dst_md5 = ($dMd5 = if ($exists) { Get-MD5Sum $dst } else { $null })
21
- chk_dirty = ($dirty = if ($sMd5 -ne $dMd5) { $true } else { $false })
22
- verifies = if ($dirty -eq $false) { $true } else { $false }
23
- target_is_folder = $dst_changed
24
- }
25
- } | Select-Object -Property chk_exists,src_md5,dst_md5,chk_dirty,verifies,target_is_folder
26
- }
27
-
28
- Function Get-MD5Sum($src) {
29
- Try {
30
- $c = [System.Security.Cryptography.MD5]::Create()
31
- $bytes = $c.ComputeHash(($in = (Get-Item $src).OpenRead()))
32
- return ([System.BitConverter]::ToString($bytes)).Replace("-", "").ToLower()
33
- }
34
- Finally {
35
- Cleanup $c
36
- Cleanup $in
37
- }
38
- }
39
-
40
- Function Unresolve-Path($path) {
41
- if ($path -eq $null) {
42
- return $null
43
- }
44
- else {
45
- return $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($path)
46
- }
47
- }
48
-
49
- Check-Files $hash_file | ConvertTo-Csv -NoTypeInformation
1
+ $hash_file = <%= hash_file %>
2
+
3
+ Function Cleanup($disposable) {
4
+ if (($disposable -ne $null) -and ($disposable.GetType().GetMethod("Dispose") -ne $null)) {
5
+ $disposable.Dispose()
6
+ }
7
+ }
8
+
9
+ Function Check-Files($h) {
10
+ return $h.GetEnumerator() | ForEach-Object {
11
+ $dst = Unresolve-Path $_.Value.target
12
+ $dst_changed = $false
13
+ if(Test-Path $dst -PathType Container) {
14
+ $dst_changed = $true
15
+ $dst = Join-Path $dst $_.Value.src_basename
16
+ }
17
+ New-Object psobject -Property @{
18
+ chk_exists = ($exists = Test-Path $dst -PathType Leaf)
19
+ src_md5 = ($sMd5 = $_.Key)
20
+ dst_md5 = ($dMd5 = if ($exists) { Get-MD5Sum $dst } else { $null })
21
+ chk_dirty = ($dirty = if ($sMd5 -ne $dMd5) { $true } else { $false })
22
+ verifies = if ($dirty -eq $false) { $true } else { $false }
23
+ target_is_folder = $dst_changed
24
+ }
25
+ } | Select-Object -Property chk_exists,src_md5,dst_md5,chk_dirty,verifies,target_is_folder
26
+ }
27
+
28
+ Function Get-MD5Sum($src) {
29
+ Try {
30
+ $c = [System.Security.Cryptography.MD5]::Create()
31
+ $bytes = $c.ComputeHash(($in = (Get-Item $src).OpenRead()))
32
+ return ([System.BitConverter]::ToString($bytes)).Replace("-", "").ToLower()
33
+ }
34
+ Finally {
35
+ Cleanup $c
36
+ Cleanup $in
37
+ }
38
+ }
39
+
40
+ Function Unresolve-Path($path) {
41
+ if ($path -eq $null) {
42
+ return $null
43
+ }
44
+ else {
45
+ return $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($path)
46
+ }
47
+ }
48
+
49
+ Check-Files $hash_file | ConvertTo-Csv -NoTypeInformation
@@ -1,13 +1,13 @@
1
- $p = $ExecutionContext.SessionState.Path
2
- $path = $p.GetUnresolvedProviderPathFromPSPath("<%= path %>")
3
-
4
- if (Test-Path $path -PathType Leaf) {
5
- $cryptoProv = [System.Security.Cryptography.MD5]::Create()
6
- $file = [System.IO.File]::Open($path,
7
- [System.IO.Filemode]::Open, [System.IO.FileAccess]::Read)
8
- $md5 = ([System.BitConverter]::ToString($cryptoProv.ComputeHash($file)))
9
- $md5 = $md5.Replace("-","").ToLower()
10
- $file.Close()
11
-
12
- Write-Output $md5
13
- }
1
+ $p = $ExecutionContext.SessionState.Path
2
+ $path = $p.GetUnresolvedProviderPathFromPSPath("<%= path %>")
3
+
4
+ if (Test-Path $path -PathType Leaf) {
5
+ $cryptoProv = [System.Security.Cryptography.<%= digest %>]::Create()
6
+ $file = [System.IO.File]::Open($path,
7
+ [System.IO.Filemode]::Open, [System.IO.FileAccess]::Read)
8
+ $digest = ([System.BitConverter]::ToString($cryptoProv.ComputeHash($file)))
9
+ $digest = $digest.Replace("-","").ToLower()
10
+ $file.Close()
11
+
12
+ Write-Output $digest
13
+ }
@@ -1,6 +1,6 @@
1
- $p = $ExecutionContext.SessionState.Path
2
- $path = $p.GetUnresolvedProviderPathFromPSPath("<%= path %>")
3
- if (!(Test-Path $path)) {
4
- New-Item -ItemType Directory -Force -Path $path | Out-Null
5
- }
6
- exit 0
1
+ $p = $ExecutionContext.SessionState.Path
2
+ $path = $p.GetUnresolvedProviderPathFromPSPath("<%= path %>")
3
+ if (!(Test-Path $path)) {
4
+ New-Item -ItemType Directory -Force -Path $path | Out-Null
5
+ }
6
+ exit 0
@@ -1,6 +1,6 @@
1
- $p = $ExecutionContext.SessionState.Path
2
- $path = $p.GetUnresolvedProviderPathFromPSPath("<%= path %>")
3
- if (Test-Path $path) {
4
- Remove-Item $path -Force -Recurse
5
- }
6
- exit 0
1
+ $p = $ExecutionContext.SessionState.Path
2
+ $path = $p.GetUnresolvedProviderPathFromPSPath("<%= path %>")
3
+ if (Test-Path $path) {
4
+ Remove-Item $path -Force -Recurse
5
+ }
6
+ exit 0
@@ -1,8 +1,8 @@
1
- $p = $ExecutionContext.SessionState.Path
2
- $path = $p.GetUnresolvedProviderPathFromPSPath("<%= path %>")
3
- if (Test-Path $path -PathType Leaf) {
4
- $bytes = [System.convert]::ToBase64String([System.IO.File]::ReadAllBytes($path))
5
- Write-Host $bytes
6
- exit 0
7
- }
8
- exit 1
1
+ $p = $ExecutionContext.SessionState.Path
2
+ $path = $p.GetUnresolvedProviderPathFromPSPath("<%= path %>")
3
+ if (Test-Path $path -PathType Leaf) {
4
+ $bytes = [System.convert]::ToBase64String([System.IO.File]::ReadAllBytes($path))
5
+ Write-Host $bytes
6
+ exit 0
7
+ }
8
+ exit 1
@@ -1,10 +1,10 @@
1
- $p = $ExecutionContext.SessionState.Path
2
- $path = $p.GetUnresolvedProviderPathFromPSPath("<%= path %>")
3
- if (Test-Path $path) {
4
- $true
5
- exit 0
6
- }
7
- else {
8
- $false
9
- exit 1
10
- }
1
+ $p = $ExecutionContext.SessionState.Path
2
+ $path = $p.GetUnresolvedProviderPathFromPSPath("<%= path %>")
3
+ if (Test-Path $path) {
4
+ $true
5
+ exit 0
6
+ }
7
+ else {
8
+ $false
9
+ exit 1
10
+ }
@@ -1,52 +1,52 @@
1
- trap {
2
- $e = $_.Exception
3
- $e.InvocationInfo.ScriptName
4
- do {
5
- $e.Message
6
- $e = $e.InnerException
7
- } while ($e)
8
- break
9
- }
10
-
11
- function folder($path){
12
- $path | ? {-not (test-path $_)} | % {$null = mkdir $_}
13
- }
14
-
15
- Function Decode-Files($hash) {
16
- foreach ($key in $hash.keys) {
17
- $value = $hash[$key]
18
- $tzip, $dst = $Value["tmpzip"], $Value["dst"]
19
- if ($tzip) {Unzip-File $tzip $dst}
20
- New-Object psobject -Property @{dst=$dst;src_md5=$key;tmpzip=$tzip}
21
- }
22
- }
23
-
24
- Function Unzip-File($src, $dst) {
25
- $unpack = $src -replace '\.zip'
26
- $dst_parent = Split-Path -Path $dst -Parent
27
- if(!(Test-Path $dst_parent)) { $dst = $dst_parent }
28
- folder $unpack, $dst
29
- try {
30
- try{
31
- [IO.Compression.ZipFile]::ExtractToDirectory($src, $unpack)
32
- }
33
- catch {
34
- Add-Type -AssemblyName System.IO.Compression.FileSystem -ErrorAction Stop
35
- [IO.Compression.ZipFile]::ExtractToDirectory($src, $unpack)
36
- }
37
- }
38
- catch {
39
- Try {
40
- $s = New-Object -ComObject Shell.Application
41
- ($s.NameSpace($unpack)).CopyHere(($s.NameSpace($src)).Items(), 0x610)
42
- }
43
- Finally {
44
- [void][Runtime.Interopservices.Marshal]::ReleaseComObject($s)
45
- }
46
- }
47
- dir $unpack | cp -dest "$dst/" -force -recurse
48
- rm $unpack -recurse -force
49
- }
50
-
51
- $hash_file = <%= hash_file %>
52
- Decode-Files $hash_file | ConvertTo-Csv -NoTypeInformation
1
+ trap {
2
+ $e = $_.Exception
3
+ $e.InvocationInfo.ScriptName
4
+ do {
5
+ $e.Message
6
+ $e = $e.InnerException
7
+ } while ($e)
8
+ break
9
+ }
10
+
11
+ function folder($path){
12
+ $path | ? {-not (test-path $_)} | % {$null = mkdir $_}
13
+ }
14
+
15
+ Function Decode-Files($hash) {
16
+ foreach ($key in $hash.keys) {
17
+ $value = $hash[$key]
18
+ $tzip, $dst = $Value["tmpzip"], $Value["dst"]
19
+ if ($tzip) {Unzip-File $tzip $dst}
20
+ New-Object psobject -Property @{dst=$dst;src_md5=$key;tmpzip=$tzip}
21
+ }
22
+ }
23
+
24
+ Function Unzip-File($src, $dst) {
25
+ $unpack = $src -replace '\.zip'
26
+ $dst_parent = Split-Path -Path $dst -Parent
27
+ if(!(Test-Path $dst_parent)) { $dst = $dst_parent }
28
+ folder $unpack, $dst
29
+ try {
30
+ try{
31
+ [IO.Compression.ZipFile]::ExtractToDirectory($src, $unpack)
32
+ }
33
+ catch {
34
+ Add-Type -AssemblyName System.IO.Compression.FileSystem -ErrorAction Stop
35
+ [IO.Compression.ZipFile]::ExtractToDirectory($src, $unpack)
36
+ }
37
+ }
38
+ catch {
39
+ Try {
40
+ $s = New-Object -ComObject Shell.Application
41
+ ($s.NameSpace($unpack)).CopyHere(($s.NameSpace($src)).Items(), 0x610)
42
+ }
43
+ Finally {
44
+ [void][Runtime.Interopservices.Marshal]::ReleaseComObject($s)
45
+ }
46
+ }
47
+ dir $unpack | cp -dest "$dst/" -force -recurse
48
+ rm $unpack -recurse -force
49
+ }
50
+
51
+ $hash_file = <%= hash_file %>
52
+ Decode-Files $hash_file | ConvertTo-Csv -NoTypeInformation
@@ -1,31 +1,31 @@
1
- # encoding: UTF-8
2
- #
3
- # Copyright 2015 Shawn Neal <sneal@sneal.net>
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
-
17
- require 'erubis'
18
-
19
- module WinRM
20
- module FS
21
- # PS1 scripts
22
- module Scripts
23
- def self.render(template, context)
24
- template_path = File.expand_path(
25
- "#{File.dirname(__FILE__)}/#{template}.ps1.erb")
26
- template = File.read(template_path)
27
- Erubis::Eruby.new(template).result(context)
28
- end
29
- end
30
- end
31
- end
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright 2015 Shawn Neal <sneal@sneal.net>
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require 'erubis'
18
+
19
+ module WinRM
20
+ module FS
21
+ # PS1 scripts
22
+ module Scripts
23
+ def self.render(template, context)
24
+ template_path = File.expand_path(
25
+ "#{File.dirname(__FILE__)}/#{template}.ps1.erb")
26
+ template = File.read(template_path)
27
+ Erubis::Eruby.new(template).result(context)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,3 +1,3 @@
1
- endpoint: "http://localhost:55985/wsman"
2
- user: vagrant
1
+ endpoint: "http://localhost:55985/wsman"
2
+ user: vagrant
3
3
  password: vagrant
@@ -1,224 +1,224 @@
1
- # encoding: UTF-8
2
- require 'pathname'
3
-
4
- describe WinRM::FS::FileManager do
5
- let(:dest_dir) { File.join(subject.temp_dir, "winrm_#{rand(2**16)}") }
6
- let(:temp_upload_dir) { '$env:TEMP/winrm-upload' }
7
- let(:spec_dir) { File.expand_path(File.dirname(File.dirname(__FILE__))) }
8
- let(:this_file) { Pathname.new(__FILE__) }
9
- let(:service) { winrm_connection }
10
-
11
- subject { WinRM::FS::FileManager.new(service) }
12
-
13
- before(:each) do
14
- expect(subject.delete(dest_dir)).to be true
15
- expect(subject.delete(temp_upload_dir)).to be true
16
- end
17
-
18
- context 'exists?' do
19
- it 'should exist' do
20
- expect(subject.exists?('c:/windows')).to be true
21
- expect(subject.exists?('c:/foobar')).to be false
22
- end
23
- end
24
-
25
- context 'create and delete dir' do
26
- it 'should create the directory recursively' do
27
- subdir = File.join(dest_dir, 'subdir1', 'subdir2')
28
- expect(subject.create_dir(subdir)).to be true
29
- expect(subject.exists?(subdir)).to be true
30
- expect(subject.create_dir(subdir)).to be true
31
- expect(subject.delete(subdir)).to be true
32
- expect(subject.exists?(subdir)).to be false
33
- end
34
- end
35
-
36
- context 'temp_dir' do
37
- it 'should return the remote users temp dir' do
38
- expect(subject.temp_dir).to match(%r{C:/Users/\S+/AppData/Local/Temp})
39
- end
40
- end
41
-
42
- context 'upload file' do
43
- let(:dest_file) { Pathname.new(File.join(dest_dir, File.basename(this_file))) }
44
-
45
- before(:each) do
46
- expect(subject.delete(dest_dir)).to be true
47
- end
48
-
49
- it 'should upload the specified file' do
50
- subject.upload(this_file, dest_file)
51
- expect(subject).to have_created(dest_file).with_content(this_file)
52
- end
53
-
54
- it 'should upload to root of the c: drive' do
55
- subject.upload(this_file, 'c:/winrmtest.rb')
56
- expect(subject).to have_created('c:/winrmtest.rb').with_content(this_file)
57
- subject.delete('c:/winrmtest.rb')
58
- end
59
-
60
- it 'should upload just filename' do
61
- subject.upload(this_file, 'winrmtest.rb')
62
- expect(subject).to have_created('winrmtest.rb').with_content(this_file)
63
- subject.delete('winrmtest.rb')
64
- end
65
-
66
- it 'should upload using relative file path' do
67
- subject.upload('./spec/integration/file_manager_spec.rb', dest_file)
68
- expect(subject).to have_created(dest_file).with_content(this_file)
69
- end
70
-
71
- it 'should upload to the specified directory' do
72
- subject.create_dir(dest_dir)
73
- subject.upload(this_file, dest_dir)
74
- expect(subject).to have_created(dest_file).with_content(this_file)
75
- end
76
-
77
- it 'should treat extensionless target as file if not an existing directory' do
78
- subject.upload(this_file, dest_dir)
79
- expect(subject).to have_created(dest_dir).with_content(this_file)
80
- end
81
-
82
- it 'should create extensionless source under target dir if target dir exists' do
83
- subject.create_dir(dest_dir)
84
- src_file = File.expand_path('../../Gemfile', File.dirname(__FILE__))
85
- subject.upload(src_file, dest_dir)
86
- expect(subject).to have_created(File.join(dest_dir, 'Gemfile')).with_content(src_file)
87
- end
88
-
89
- it 'should upload to the specified directory with env var' do
90
- subject.upload(this_file, '$env:Temp')
91
- expected_dest_file = File.join(subject.temp_dir, File.basename(this_file))
92
- expect(subject).to have_created(expected_dest_file).with_content(this_file)
93
- end
94
-
95
- it 'should upload to Program Files sub dir' do
96
- subject.create_dir('$env:ProgramFiles/foo')
97
- subject.upload(this_file, '$env:ProgramFiles/foo')
98
- expect(subject).to have_created('c:/Program Files/foo/file_manager_spec.rb') \
99
- .with_content(this_file)
100
- end
101
-
102
- it 'should upload to the specified nested directory' do
103
- dest_sub_dir = File.join(dest_dir, 'subdir')
104
- subject.create_dir(dest_sub_dir)
105
- dest_sub_dir_file = File.join(dest_sub_dir, File.basename(this_file))
106
- subject.upload(this_file, dest_sub_dir)
107
- expect(subject).to have_created(dest_sub_dir_file).with_content(this_file)
108
- end
109
-
110
- it 'yields progress data' do
111
- block_called = false
112
- total_bytes_copied = 0
113
- total = subject.upload(this_file, dest_file) do \
114
- |bytes_copied, total_bytes, local_path, remote_path|
115
- expect(total_bytes).to be > 0
116
- total_bytes_copied = bytes_copied
117
- expect(local_path).to eq(this_file.to_s)
118
- expect(remote_path).to eq(dest_file.to_s)
119
- block_called = true
120
- end
121
- expect(total_bytes_copied).to eq(total)
122
- expect(block_called).to be true
123
- expect(total).to be > 0
124
- end
125
-
126
- it 'should not upload when content matches' do
127
- subject.upload(this_file, dest_dir)
128
- bytes_uploaded = subject.upload(this_file, dest_dir)
129
- expect(bytes_uploaded).to eq 0
130
- end
131
-
132
- it 'should upload when content differs' do
133
- matchers_file = File.join(spec_dir, 'matchers.rb')
134
- subject.upload(matchers_file, dest_file)
135
- bytes_uploaded = subject.upload(this_file, dest_file)
136
- expect(bytes_uploaded).to be > 0
137
- end
138
-
139
- it 'raises WinRMUploadError when a bad source path is specified' do
140
- expect { subject.upload('c:/some/non-existant/path/foo', dest_file) }.to raise_error
141
- end
142
- end
143
-
144
- context 'upload empty file' do
145
- let(:empty_src_file) { Tempfile.new('empty') }
146
- let(:dest_file) { File.join(dest_dir, 'emptyfile.txt') }
147
-
148
- it 'creates a new empty file' do
149
- expect(subject.upload(empty_src_file.path, dest_file)).to be 0
150
- expect(subject).to have_created(dest_file).with_content('')
151
- end
152
-
153
- it 'overwrites an existing file' do
154
- expect(subject.upload(this_file, dest_file)).to be > 0
155
- expect(subject.upload(empty_src_file.path, dest_file)).to be 0
156
- expect(subject).to have_created(dest_file).with_content('')
157
- end
158
- end
159
-
160
- context 'upload directory' do
161
- let(:root_dir) { File.expand_path('../../', File.dirname(__FILE__)) }
162
- let(:winrm_fs_dir) { File.join(root_dir, 'lib/winrm-fs') }
163
- let(:core_dir) { File.join(root_dir, 'lib/winrm-fs/core') }
164
- let(:scripts_dir) { File.join(root_dir, 'lib/winrm-fs/scripts') }
165
-
166
- it 'copies the directory contents recursively when directory does not exist' do
167
- bytes_uploaded = subject.upload(winrm_fs_dir, dest_dir)
168
- expect(bytes_uploaded).to be > 0
169
-
170
- Dir.glob(winrm_fs_dir + '/**/*.rb').each do |host_file|
171
- host_file_rel = Pathname.new(host_file).relative_path_from(
172
- Pathname.new(winrm_fs_dir)
173
- ).to_s
174
- remote_file = File.join(dest_dir, host_file_rel)
175
- expect(subject).to have_created(remote_file).with_content(host_file)
176
- end
177
- end
178
-
179
- it 'copies the directory recursively when directory does exist' do
180
- subject.create_dir(dest_dir)
181
- bytes_uploaded = subject.upload(winrm_fs_dir, dest_dir)
182
- expect(bytes_uploaded).to be > 0
183
-
184
- Dir.glob(winrm_fs_dir + '/**/*.rb').each do |host_file|
185
- host_file_rel = Pathname.new(host_file).relative_path_from(
186
- Pathname.new(winrm_fs_dir).dirname
187
- ).to_s
188
- remote_file = File.join(dest_dir, host_file_rel)
189
- expect(subject).to have_created(remote_file).with_content(host_file)
190
- end
191
- end
192
-
193
- it 'does not copy the directory when content is the same' do
194
- subject.upload(winrm_fs_dir, dest_dir)
195
- bytes_uploaded = subject.upload(winrm_fs_dir, dest_dir)
196
- expect(bytes_uploaded).to eq 0
197
- end
198
-
199
- it 'unzips the directory when cached content is the same' do
200
- subject.upload(winrm_fs_dir, dest_dir)
201
- subject.delete(dest_dir)
202
- expect(subject.exists?(dest_dir)).to be false
203
- subject.upload(winrm_fs_dir, dest_dir)
204
- expect(subject.exists?(dest_dir)).to be true
205
- end
206
-
207
- it 'unzips multiple directories when cached content is the same for all' do
208
- subject.create_dir(dest_dir)
209
- subject.upload([core_dir, scripts_dir], dest_dir)
210
- subject.delete(dest_dir)
211
- expect(subject.exists?(dest_dir)).to be false
212
- subject.create_dir(dest_dir)
213
- subject.upload([core_dir, scripts_dir], dest_dir)
214
- expect(subject.exists?(File.join(dest_dir, 'core'))).to be true
215
- expect(subject.exists?(File.join(dest_dir, 'scripts'))).to be true
216
- end
217
-
218
- it 'copies the directory when content differs' do
219
- subject.upload(winrm_fs_dir, dest_dir)
220
- bytes_uploaded = subject.upload(core_dir, dest_dir)
221
- expect(bytes_uploaded).to be > 0
222
- end
223
- end
224
- end
1
+ # encoding: UTF-8
2
+ require 'pathname'
3
+
4
+ describe WinRM::FS::FileManager do
5
+ let(:dest_dir) { File.join(subject.temp_dir, "winrm_#{rand(2**16)}") }
6
+ let(:temp_upload_dir) { '$env:TEMP/winrm-upload' }
7
+ let(:spec_dir) { File.expand_path(File.dirname(File.dirname(__FILE__))) }
8
+ let(:this_file) { Pathname.new(__FILE__) }
9
+ let(:service) { winrm_connection }
10
+
11
+ subject { WinRM::FS::FileManager.new(service) }
12
+
13
+ before(:each) do
14
+ expect(subject.delete(dest_dir)).to be true
15
+ expect(subject.delete(temp_upload_dir)).to be true
16
+ end
17
+
18
+ context 'exists?' do
19
+ it 'should exist' do
20
+ expect(subject.exists?('c:/windows')).to be true
21
+ expect(subject.exists?('c:/foobar')).to be false
22
+ end
23
+ end
24
+
25
+ context 'create and delete dir' do
26
+ it 'should create the directory recursively' do
27
+ subdir = File.join(dest_dir, 'subdir1', 'subdir2')
28
+ expect(subject.create_dir(subdir)).to be true
29
+ expect(subject.exists?(subdir)).to be true
30
+ expect(subject.create_dir(subdir)).to be true
31
+ expect(subject.delete(subdir)).to be true
32
+ expect(subject.exists?(subdir)).to be false
33
+ end
34
+ end
35
+
36
+ context 'temp_dir' do
37
+ it 'should return the remote users temp dir' do
38
+ expect(subject.temp_dir).to match(%r{C:/Users/\S+/AppData/Local/Temp})
39
+ end
40
+ end
41
+
42
+ context 'upload file' do
43
+ let(:dest_file) { Pathname.new(File.join(dest_dir, File.basename(this_file))) }
44
+
45
+ before(:each) do
46
+ expect(subject.delete(dest_dir)).to be true
47
+ end
48
+
49
+ it 'should upload the specified file' do
50
+ subject.upload(this_file, dest_file)
51
+ expect(subject).to have_created(dest_file).with_content(this_file)
52
+ end
53
+
54
+ it 'should upload to root of the c: drive' do
55
+ subject.upload(this_file, 'c:/winrmtest.rb')
56
+ expect(subject).to have_created('c:/winrmtest.rb').with_content(this_file)
57
+ subject.delete('c:/winrmtest.rb')
58
+ end
59
+
60
+ it 'should upload just filename' do
61
+ subject.upload(this_file, 'winrmtest.rb')
62
+ expect(subject).to have_created('winrmtest.rb').with_content(this_file)
63
+ subject.delete('winrmtest.rb')
64
+ end
65
+
66
+ it 'should upload using relative file path' do
67
+ subject.upload('./spec/integration/file_manager_spec.rb', dest_file)
68
+ expect(subject).to have_created(dest_file).with_content(this_file)
69
+ end
70
+
71
+ it 'should upload to the specified directory' do
72
+ subject.create_dir(dest_dir)
73
+ subject.upload(this_file, dest_dir)
74
+ expect(subject).to have_created(dest_file).with_content(this_file)
75
+ end
76
+
77
+ it 'should treat extensionless target as file if not an existing directory' do
78
+ subject.upload(this_file, dest_dir)
79
+ expect(subject).to have_created(dest_dir).with_content(this_file)
80
+ end
81
+
82
+ it 'should create extensionless source under target dir if target dir exists' do
83
+ subject.create_dir(dest_dir)
84
+ src_file = File.expand_path('../../Gemfile', File.dirname(__FILE__))
85
+ subject.upload(src_file, dest_dir)
86
+ expect(subject).to have_created(File.join(dest_dir, 'Gemfile')).with_content(src_file)
87
+ end
88
+
89
+ it 'should upload to the specified directory with env var' do
90
+ subject.upload(this_file, '$env:Temp')
91
+ expected_dest_file = File.join(subject.temp_dir, File.basename(this_file))
92
+ expect(subject).to have_created(expected_dest_file).with_content(this_file)
93
+ end
94
+
95
+ it 'should upload to Program Files sub dir' do
96
+ subject.create_dir('$env:ProgramFiles/foo')
97
+ subject.upload(this_file, '$env:ProgramFiles/foo')
98
+ expect(subject).to have_created('c:/Program Files/foo/file_manager_spec.rb') \
99
+ .with_content(this_file)
100
+ end
101
+
102
+ it 'should upload to the specified nested directory' do
103
+ dest_sub_dir = File.join(dest_dir, 'subdir')
104
+ subject.create_dir(dest_sub_dir)
105
+ dest_sub_dir_file = File.join(dest_sub_dir, File.basename(this_file))
106
+ subject.upload(this_file, dest_sub_dir)
107
+ expect(subject).to have_created(dest_sub_dir_file).with_content(this_file)
108
+ end
109
+
110
+ it 'yields progress data' do
111
+ block_called = false
112
+ total_bytes_copied = 0
113
+ total = subject.upload(this_file, dest_file) do \
114
+ |bytes_copied, total_bytes, local_path, remote_path|
115
+ expect(total_bytes).to be > 0
116
+ total_bytes_copied = bytes_copied
117
+ expect(local_path).to eq(this_file.to_s)
118
+ expect(remote_path).to eq(dest_file.to_s)
119
+ block_called = true
120
+ end
121
+ expect(total_bytes_copied).to eq(total)
122
+ expect(block_called).to be true
123
+ expect(total).to be > 0
124
+ end
125
+
126
+ it 'should not upload when content matches' do
127
+ subject.upload(this_file, dest_dir)
128
+ bytes_uploaded = subject.upload(this_file, dest_dir)
129
+ expect(bytes_uploaded).to eq 0
130
+ end
131
+
132
+ it 'should upload when content differs' do
133
+ matchers_file = File.join(spec_dir, 'matchers.rb')
134
+ subject.upload(matchers_file, dest_file)
135
+ bytes_uploaded = subject.upload(this_file, dest_file)
136
+ expect(bytes_uploaded).to be > 0
137
+ end
138
+
139
+ it 'raises WinRMUploadError when a bad source path is specified' do
140
+ expect { subject.upload('c:/some/non-existant/path/foo', dest_file) }.to raise_error
141
+ end
142
+ end
143
+
144
+ context 'upload empty file' do
145
+ let(:empty_src_file) { Tempfile.new('empty') }
146
+ let(:dest_file) { File.join(dest_dir, 'emptyfile.txt') }
147
+
148
+ it 'creates a new empty file' do
149
+ expect(subject.upload(empty_src_file.path, dest_file)).to be 0
150
+ expect(subject).to have_created(dest_file).with_content('')
151
+ end
152
+
153
+ it 'overwrites an existing file' do
154
+ expect(subject.upload(this_file, dest_file)).to be > 0
155
+ expect(subject.upload(empty_src_file.path, dest_file)).to be 0
156
+ expect(subject).to have_created(dest_file).with_content('')
157
+ end
158
+ end
159
+
160
+ context 'upload directory' do
161
+ let(:root_dir) { File.expand_path('../../', File.dirname(__FILE__)) }
162
+ let(:winrm_fs_dir) { File.join(root_dir, 'lib/winrm-fs') }
163
+ let(:core_dir) { File.join(root_dir, 'lib/winrm-fs/core') }
164
+ let(:scripts_dir) { File.join(root_dir, 'lib/winrm-fs/scripts') }
165
+
166
+ it 'copies the directory contents recursively when directory does not exist' do
167
+ bytes_uploaded = subject.upload(winrm_fs_dir, dest_dir)
168
+ expect(bytes_uploaded).to be > 0
169
+
170
+ Dir.glob(winrm_fs_dir + '/**/*.rb').each do |host_file|
171
+ host_file_rel = Pathname.new(host_file).relative_path_from(
172
+ Pathname.new(winrm_fs_dir)
173
+ ).to_s
174
+ remote_file = File.join(dest_dir, host_file_rel)
175
+ expect(subject).to have_created(remote_file).with_content(host_file)
176
+ end
177
+ end
178
+
179
+ it 'copies the directory recursively when directory does exist' do
180
+ subject.create_dir(dest_dir)
181
+ bytes_uploaded = subject.upload(winrm_fs_dir, dest_dir)
182
+ expect(bytes_uploaded).to be > 0
183
+
184
+ Dir.glob(winrm_fs_dir + '/**/*.rb').each do |host_file|
185
+ host_file_rel = Pathname.new(host_file).relative_path_from(
186
+ Pathname.new(winrm_fs_dir).dirname
187
+ ).to_s
188
+ remote_file = File.join(dest_dir, host_file_rel)
189
+ expect(subject).to have_created(remote_file).with_content(host_file)
190
+ end
191
+ end
192
+
193
+ it 'does not copy the directory when content is the same' do
194
+ subject.upload(winrm_fs_dir, dest_dir)
195
+ bytes_uploaded = subject.upload(winrm_fs_dir, dest_dir)
196
+ expect(bytes_uploaded).to eq 0
197
+ end
198
+
199
+ it 'unzips the directory when cached content is the same' do
200
+ subject.upload(winrm_fs_dir, dest_dir)
201
+ subject.delete(dest_dir)
202
+ expect(subject.exists?(dest_dir)).to be false
203
+ subject.upload(winrm_fs_dir, dest_dir)
204
+ expect(subject.exists?(dest_dir)).to be true
205
+ end
206
+
207
+ it 'unzips multiple directories when cached content is the same for all' do
208
+ subject.create_dir(dest_dir)
209
+ subject.upload([core_dir, scripts_dir], dest_dir)
210
+ subject.delete(dest_dir)
211
+ expect(subject.exists?(dest_dir)).to be false
212
+ subject.create_dir(dest_dir)
213
+ subject.upload([core_dir, scripts_dir], dest_dir)
214
+ expect(subject.exists?(File.join(dest_dir, 'core'))).to be true
215
+ expect(subject.exists?(File.join(dest_dir, 'scripts'))).to be true
216
+ end
217
+
218
+ it 'copies the directory when content differs' do
219
+ subject.upload(winrm_fs_dir, dest_dir)
220
+ bytes_uploaded = subject.upload(core_dir, dest_dir)
221
+ expect(bytes_uploaded).to be > 0
222
+ end
223
+ end
224
+ end