winrm-fs 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,48 +1,54 @@
1
- $hash_file = "<%= hash_file %>"
2
-
3
- Function Cleanup($o) { if (($o -ne $null) -and ($o.GetType().GetMethod("Dispose") -ne $null)) { $o.Dispose() } }
4
-
5
- Function Decode-Base64File($src, $dst) {
6
- Try {
7
- $in = (Get-Item $src).OpenRead()
8
- $b64 = New-Object -TypeName System.Security.Cryptography.FromBase64Transform
9
- $m = [System.Security.Cryptography.CryptoStreamMode]::Read
10
- $d = New-Object -TypeName System.Security.Cryptography.CryptoStream $in,$b64,$m
11
- Copy-Stream $d ($out = [System.IO.File]::OpenWrite($dst))
12
- } Finally { Cleanup $in; Cleanup $out; Cleanup $d }
13
- }
14
-
15
- Function Copy-Stream($src, $dst) { $b = New-Object Byte[] 4096; while (($i = $src.Read($b, 0, $b.Length)) -ne 0) { $dst.Write($b, 0, $i) } }
16
-
17
- Function Check-Files($h) {
18
- return $h.GetEnumerator() | ForEach-Object {
19
- $dst = Unresolve-Path $_.Key
20
- New-Object psobject -Property @{
21
- chk_exists = ($exists = Test-Path $dst -PathType Leaf)
22
- src_md5 = ($sMd5 = $_.Value)
23
- dst_md5 = ($dMd5 = if ($exists) { Get-MD5Sum $dst } else { $null })
24
- chk_dirty = ($dirty = if ($sMd5 -ne $dMd5) { $true } else { $false })
25
- verifies = if ($dirty -eq $false) { $true } else { $false }
26
- }
27
- } | Select-Object -Property chk_exists,src_md5,dst_md5,chk_dirty,verifies
28
- }
29
-
30
- Function Get-MD5Sum($src) {
31
- Try {
32
- $c = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
33
- $bytes = $c.ComputeHash(($in = (Get-Item $src).OpenRead()))
34
- return ([System.BitConverter]::ToString($bytes)).Replace("-", "").ToLower()
35
- } Finally { Cleanup $c; Cleanup $in }
36
- }
37
-
38
- Function Invoke-Input($in) {
39
- $in = Unresolve-Path $in
40
- Decode-Base64File $in ($decoded = "$($in).ps1")
41
- $expr = Get-Content $decoded | Out-String
42
- Remove-Item $in,$decoded -Force
43
- return Invoke-Expression "$expr"
44
- }
45
-
46
- Function Unresolve-Path($p) { if ($p -eq $null) { return $null } else { return $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($p) } }
47
-
48
- Check-Files (Invoke-Input $hash_file) | ConvertTo-Csv -NoTypeInformation
1
+ $hash_file = "<%= hash_file %>"
2
+
3
+ Function Cleanup($o) { if (($o -ne $null) -and ($o.GetType().GetMethod("Dispose") -ne $null)) { $o.Dispose() } }
4
+
5
+ Function Decode-Base64File($src, $dst) {
6
+ Try {
7
+ $in = (Get-Item $src).OpenRead()
8
+ $b64 = New-Object -TypeName System.Security.Cryptography.FromBase64Transform
9
+ $m = [System.Security.Cryptography.CryptoStreamMode]::Read
10
+ $d = New-Object -TypeName System.Security.Cryptography.CryptoStream $in,$b64,$m
11
+ Copy-Stream $d ($out = [System.IO.File]::OpenWrite($dst))
12
+ } Finally { Cleanup $in; Cleanup $out; Cleanup $d }
13
+ }
14
+
15
+ Function Copy-Stream($src, $dst) { $b = New-Object Byte[] 4096; while (($i = $src.Read($b, 0, $b.Length)) -ne 0) { $dst.Write($b, 0, $i) } }
16
+
17
+ Function Check-Files($h) {
18
+ return $h.GetEnumerator() | ForEach-Object {
19
+ $dst = Unresolve-Path $_.Value.target
20
+ $dst_changed = $false
21
+ if(Test-Path $dst -PathType Container) {
22
+ $dst_changed = $true
23
+ $dst = Join-Path $dst $_.Value.src_basename
24
+ }
25
+ New-Object psobject -Property @{
26
+ chk_exists = ($exists = Test-Path $dst -PathType Leaf)
27
+ src_md5 = ($sMd5 = $_.Key)
28
+ dst_md5 = ($dMd5 = if ($exists) { Get-MD5Sum $dst } else { $null })
29
+ chk_dirty = ($dirty = if ($sMd5 -ne $dMd5) { $true } else { $false })
30
+ verifies = if ($dirty -eq $false) { $true } else { $false }
31
+ dst = if($dst_changed) { $dst } else { $_.Value.dst }
32
+ }
33
+ } | Select-Object -Property chk_exists,src_md5,dst_md5,chk_dirty,verifies,dst
34
+ }
35
+
36
+ Function Get-MD5Sum($src) {
37
+ Try {
38
+ $c = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
39
+ $bytes = $c.ComputeHash(($in = (Get-Item $src).OpenRead()))
40
+ return ([System.BitConverter]::ToString($bytes)).Replace("-", "").ToLower()
41
+ } Finally { Cleanup $c; Cleanup $in }
42
+ }
43
+
44
+ Function Invoke-Input($in) {
45
+ $in = Unresolve-Path $in
46
+ Decode-Base64File $in ($decoded = "$($in).ps1")
47
+ $expr = Get-Content $decoded | Out-String
48
+ Remove-Item $in,$decoded -Force
49
+ return Invoke-Expression "$expr"
50
+ }
51
+
52
+ Function Unresolve-Path($p) { if ($p -eq $null) { return $null } else { return $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($p) } }
53
+
54
+ Check-Files (Invoke-Input $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 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
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 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
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,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,58 +1,58 @@
1
- trap {$e = $_.Exception; $e.InvocationInfo.ScriptName; do {$e.Message; $e = $e.InnerException} while ($e); break;}
2
- function Decode-Base64File($src, $dst) {folder (split-path $dst);sc -force -Encoding Byte -Path $dst -Value ([Convert]::FromBase64String([IO.File]::ReadAllLines($src)))}
3
- function Copy-Stream($src, $dst) {$b = New-Object Byte[] 4096; while (($i = $src.Read($b, 0, $b.Length)) -ne 0) { $dst.Write($b, 0, $i) } }
4
- function Resolve-ProviderPath{ $input | % {if ($_){(Resolve-Path $_).ProviderPath} else{$null}} }
5
- function Get-FrameworkVersion { "Full", "Client" | % {([version](gp "HKLM:\Software\Microsoft\NET Framework Setup\NDP\v4\$_").Version)} | select -first 1}
6
- function Test-NETStack($Version){ Get-FrameworkVersion -ge $Version }
7
- function Test-IOCompression {($PSVersionTable.PSVersion.Major -ge 3) -and (Test-NETStack '4.5')}
8
- function folder($path){ $path | ? {-not (test-path $_)} | % {$null = mkdir $_}}
9
- function disposable($o){($o -is [IDisposable]) -and (($o | gm | %{$_.name}) -contains 'Dispose')}
10
- function use($obj, [scriptblock]$sb){try {& $sb} catch [exception]{throw $_} finally {if (disposable $obj) {$obj.Dispose()}} }
11
- set-alias RPP -Value Resolve-ProviderPath
12
-
13
- Function Decode-Files($hash) {
14
- foreach ($key in $hash.keys) {
15
- $value = $hash[$key]
16
- $tmp, $tzip, $dst = $Key, $Value["tmpzip"], $Value["dst"]
17
- if(!$tmp.StartsWith("clean")) {
18
- $sMd5 = (gi $tmp).BaseName.Replace("b64-", "")
19
- $decoded = if ($tzip -ne $null) { $tzip } else { $dst }
20
- Decode-Base64File $tmp $decoded
21
- rm $tmp -Force
22
- $dMd5 = Get-MD5Sum $decoded
23
- $verifies = $sMd5 -like $dMd5
24
- }
25
- if ($tzip) {Unzip-File $tzip $dst}
26
- New-Object psobject -Property @{dst=$dst;verifies=$verifies;src_md5=$sMd5;dst_md5=$dMd5;tmpfile=$tmp;tmpzip=$tzip}
27
- }
28
- }
29
-
30
- Function Get-MD5Sum($src) {
31
- if ($src -and (test-path $src)) {
32
- use ($c = New-Object -TypeName Security.Cryptography.MD5CryptoServiceProvider) {
33
- use ($in = (gi $src).OpenRead()) {([BitConverter]::ToString($c.ComputeHash($in))).Replace("-", "").ToLower()}}
34
- }
35
- }
36
-
37
- Function Invoke-Input($in) {
38
- $in = $in | rpp
39
- $d = "$($in).ps1"
40
- Decode-Base64File $in $d
41
- $expr = gc $d | Out-String
42
- rm $in,$d -Force
43
- iex "$expr"
44
- }
45
-
46
- Function Unzip-File($src, $dst) {
47
- $unpack = $src -replace '\.zip'
48
- $dst_parent = Split-Path -Path $dst -Parent
49
- if(!(Test-Path $dst_parent)) { $dst = $dst_parent }
50
- folder $unpack, $dst
51
- if (Test-IOCompression) {Add-Type -AN System.IO.Compression.FileSystem; [IO.Compression.ZipFile]::ExtractToDirectory($src, $unpack)}
52
- else {Try {$s = New-Object -ComObject Shell.Application; ($s.NameSpace($unpack)).CopyHere(($s.NameSpace($src)).Items(), 0x610)} Finally {[void][Runtime.Interopservices.Marshal]::ReleaseComObject($s)}}
53
- dir $unpack | cp -dest "$dst/" -force -recurse
54
- rm $unpack -recurse -force
55
- }
56
-
57
- $hash_file = "<%= hash_file %>"
58
- Decode-Files (Invoke-Input $hash_file) | ConvertTo-Csv -NoTypeInformation
1
+ trap {$e = $_.Exception; $e.InvocationInfo.ScriptName; do {$e.Message; $e = $e.InnerException} while ($e); break;}
2
+ function Decode-Base64File($src, $dst) {folder (split-path $dst);sc -force -Encoding Byte -Path $dst -Value ([Convert]::FromBase64String([IO.File]::ReadAllLines($src)))}
3
+ function Copy-Stream($src, $dst) {$b = New-Object Byte[] 4096; while (($i = $src.Read($b, 0, $b.Length)) -ne 0) { $dst.Write($b, 0, $i) } }
4
+ function Resolve-ProviderPath{ $input | % {if ($_){(Resolve-Path $_).ProviderPath} else{$null}} }
5
+ function Get-FrameworkVersion { "Full", "Client" | % {([version](gp "HKLM:\Software\Microsoft\NET Framework Setup\NDP\v4\$_").Version)} | select -first 1}
6
+ function Test-NETStack($Version){ Get-FrameworkVersion -ge $Version }
7
+ function Test-IOCompression {($PSVersionTable.PSVersion.Major -ge 3) -and (Test-NETStack '4.5')}
8
+ function folder($path){ $path | ? {-not (test-path $_)} | % {$null = mkdir $_}}
9
+ function disposable($o){($o -is [IDisposable]) -and (($o | gm | %{$_.name}) -contains 'Dispose')}
10
+ function use($obj, [scriptblock]$sb){try {& $sb} catch [exception]{throw $_} finally {if (disposable $obj) {$obj.Dispose()}} }
11
+ set-alias RPP -Value Resolve-ProviderPath
12
+
13
+ Function Decode-Files($hash) {
14
+ foreach ($key in $hash.keys) {
15
+ $value = $hash[$key]
16
+ $tmp, $tzip, $dst = $Key, $Value["tmpzip"], $Value["dst"]
17
+ if(!$tmp.StartsWith("clean")) {
18
+ $sMd5 = (gi $tmp).BaseName.Replace("b64-", "")
19
+ $decoded = if ($tzip -ne $null) { $tzip } else { $dst }
20
+ Decode-Base64File $tmp $decoded
21
+ rm $tmp -Force
22
+ $dMd5 = Get-MD5Sum $decoded
23
+ $verifies = $sMd5 -like $dMd5
24
+ }
25
+ if ($tzip) {Unzip-File $tzip $dst}
26
+ New-Object psobject -Property @{dst=$dst;verifies=$verifies;src_md5=$sMd5;dst_md5=$dMd5;tmpfile=$tmp;tmpzip=$tzip}
27
+ }
28
+ }
29
+
30
+ Function Get-MD5Sum($src) {
31
+ if ($src -and (test-path $src)) {
32
+ use ($c = New-Object -TypeName Security.Cryptography.MD5CryptoServiceProvider) {
33
+ use ($in = (gi $src).OpenRead()) {([BitConverter]::ToString($c.ComputeHash($in))).Replace("-", "").ToLower()}}
34
+ }
35
+ }
36
+
37
+ Function Invoke-Input($in) {
38
+ $in = $in | rpp
39
+ $d = "$($in).ps1"
40
+ Decode-Base64File $in $d
41
+ $expr = gc $d | Out-String
42
+ rm $in,$d -Force
43
+ iex "$expr"
44
+ }
45
+
46
+ Function Unzip-File($src, $dst) {
47
+ $unpack = $src -replace '\.zip'
48
+ $dst_parent = Split-Path -Path $dst -Parent
49
+ if(!(Test-Path $dst_parent)) { $dst = $dst_parent }
50
+ folder $unpack, $dst
51
+ if (Test-IOCompression) {Add-Type -AN System.IO.Compression.FileSystem; [IO.Compression.ZipFile]::ExtractToDirectory($src, $unpack)}
52
+ else {Try {$s = New-Object -ComObject Shell.Application; ($s.NameSpace($unpack)).CopyHere(($s.NameSpace($src)).Items(), 0x610)} Finally {[void][Runtime.Interopservices.Marshal]::ReleaseComObject($s)}}
53
+ dir $unpack | cp -dest "$dst/" -force -recurse
54
+ rm $unpack -recurse -force
55
+ }
56
+
57
+ $hash_file = "<%= hash_file %>"
58
+ Decode-Files (Invoke-Input $hash_file) | ConvertTo-Csv -NoTypeInformation
@@ -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,8 +1,8 @@
1
- $p = $ExecutionContext.SessionState.Path
2
- $path = $p.GetUnresolvedProviderPathFromPSPath("<%= path %>")
3
- if (Test-Path $path) {
4
- exit 0
5
- }
6
- else {
7
- exit 1
8
- }
1
+ $p = $ExecutionContext.SessionState.Path
2
+ $path = $p.GetUnresolvedProviderPathFromPSPath("<%= path %>")
3
+ if (Test-Path $path) {
4
+ exit 0
5
+ }
6
+ else {
7
+ exit 1
8
+ }
@@ -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,5 +1,5 @@
1
- auth_type: plaintext
2
- endpoint: "http://192.168.137.20:5985/wsman"
3
- options:
4
- user: vagrant
1
+ auth_type: plaintext
2
+ endpoint: "http://192.168.137.20:5985/wsman"
3
+ options:
4
+ user: vagrant
5
5
  pass: vagrant
@@ -1,203 +1,218 @@
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) { __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) { 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 using relative file path' do
61
- subject.upload('./spec/integration/file_manager_spec.rb', dest_file)
62
- expect(subject).to have_created(dest_file).with_content(this_file)
63
- end
64
-
65
- it 'should upload to the specified directory' do
66
- subject.upload(this_file, dest_dir)
67
- expect(subject).to have_created(dest_file).with_content(this_file)
68
- end
69
-
70
- it 'should upload to the specified directory with env var' do
71
- subject.upload(this_file, '$env:Temp')
72
- expected_dest_file = File.join(subject.temp_dir, File.basename(this_file))
73
- expect(subject).to have_created(expected_dest_file).with_content(this_file)
74
- end
75
-
76
- it 'should upload to Program Files sub dir' do
77
- subject.upload(this_file, '$env:ProgramFiles/foo')
78
- expect(subject).to have_created('c:/Program Files/foo/file_manager_spec.rb') \
79
- .with_content(this_file)
80
- end
81
-
82
- it 'should upload to the specified nested directory' do
83
- dest_sub_dir = File.join(dest_dir, 'subdir')
84
- dest_sub_dir_file = File.join(dest_sub_dir, File.basename(this_file))
85
- subject.upload(this_file, dest_sub_dir)
86
- expect(subject).to have_created(dest_sub_dir_file).with_content(this_file)
87
- end
88
-
89
- it 'yields progress data' do
90
- block_called = false
91
- total_bytes_copied = 0
92
- total = subject.upload(this_file, dest_file) do \
93
- |bytes_copied, total_bytes, local_path, remote_path|
94
- expect(total_bytes).to be > 0
95
- total_bytes_copied = bytes_copied
96
- expect(local_path).to eq(this_file)
97
- expect(remote_path).to eq(dest_file)
98
- block_called = true
99
- end
100
- expect(total_bytes_copied).to eq(total)
101
- expect(block_called).to be true
102
- expect(total).to be > 0
103
- end
104
-
105
- it 'should not upload when content matches' do
106
- subject.upload(this_file, dest_dir)
107
- bytes_uploaded = subject.upload(this_file, dest_dir)
108
- expect(bytes_uploaded).to eq 0
109
- end
110
-
111
- it 'should upload when content differs' do
112
- matchers_file = File.join(spec_dir, 'matchers.rb')
113
- subject.upload(matchers_file, dest_file)
114
- bytes_uploaded = subject.upload(this_file, dest_file)
115
- expect(bytes_uploaded).to be > 0
116
- end
117
-
118
- it 'raises WinRMUploadError when a bad source path is specified' do
119
- expect { subject.upload('c:/some/non-existant/path/foo', dest_file) }.to raise_error
120
- end
121
- end
122
-
123
- context 'upload empty file' do
124
- let(:empty_src_file) { Tempfile.new('empty') }
125
- let(:dest_file) { File.join(dest_dir, 'emptyfile.txt') }
126
-
127
- it 'creates a new empty file' do
128
- expect(subject.upload(empty_src_file.path, dest_file)).to be 0
129
- expect(subject).to have_created(dest_file).with_content('')
130
- end
131
-
132
- it 'overwrites an existing file' do
133
- expect(subject.upload(this_file, dest_file)).to be > 0
134
- expect(subject.upload(empty_src_file.path, dest_file)).to be 0
135
- expect(subject).to have_created(dest_file).with_content('')
136
- end
137
- end
138
-
139
- context 'upload directory' do
140
- let(:root_dir) { File.expand_path('../../', File.dirname(__FILE__)) }
141
- let(:winrm_fs_dir) { File.join(root_dir, 'lib/winrm-fs') }
142
- let(:core_dir) { File.join(root_dir, 'lib/winrm-fs/core') }
143
- let(:scripts_dir) { File.join(root_dir, 'lib/winrm-fs/scripts') }
144
-
145
- it 'copies the directory contents recursively when directory does not exist' do
146
- bytes_uploaded = subject.upload(winrm_fs_dir, dest_dir)
147
- expect(bytes_uploaded).to be > 0
148
-
149
- Dir.glob(winrm_fs_dir + '/**/*.rb').each do |host_file|
150
- host_file_rel = Pathname.new(host_file).relative_path_from(
151
- Pathname.new(winrm_fs_dir)
152
- ).to_s
153
- remote_file = File.join(dest_dir, host_file_rel)
154
- expect(subject).to have_created(remote_file).with_content(host_file)
155
- end
156
- end
157
-
158
- it 'copies the directory recursively when directory does exist' do
159
- subject.create_dir(dest_dir)
160
- bytes_uploaded = subject.upload(winrm_fs_dir, dest_dir)
161
- expect(bytes_uploaded).to be > 0
162
-
163
- Dir.glob(winrm_fs_dir + '/**/*.rb').each do |host_file|
164
- host_file_rel = Pathname.new(host_file).relative_path_from(
165
- Pathname.new(winrm_fs_dir).dirname
166
- ).to_s
167
- remote_file = File.join(dest_dir, host_file_rel)
168
- expect(subject).to have_created(remote_file).with_content(host_file)
169
- end
170
- end
171
-
172
- it 'does not copy the directory when content is the same' do
173
- subject.upload(winrm_fs_dir, dest_dir)
174
- bytes_uploaded = subject.upload(winrm_fs_dir, dest_dir)
175
- expect(bytes_uploaded).to eq 0
176
- end
177
-
178
- it 'unzips the directory when cached content is the same' do
179
- subject.upload(winrm_fs_dir, dest_dir)
180
- subject.delete(dest_dir)
181
- expect(subject.exists?(dest_dir)).to be false
182
- subject.upload(winrm_fs_dir, dest_dir)
183
- expect(subject.exists?(dest_dir)).to be true
184
- end
185
-
186
- it 'unzips multiple directories when cached content is the same for all' do
187
- subject.create_dir(dest_dir)
188
- subject.upload([core_dir, scripts_dir], dest_dir)
189
- subject.delete(dest_dir)
190
- expect(subject.exists?(dest_dir)).to be false
191
- subject.create_dir(dest_dir)
192
- subject.upload([core_dir, scripts_dir], dest_dir)
193
- expect(subject.exists?(File.join(dest_dir, 'core'))).to be true
194
- expect(subject.exists?(File.join(dest_dir, 'scripts'))).to be true
195
- end
196
-
197
- it 'copies the directory when content differs' do
198
- subject.upload(winrm_fs_dir, dest_dir)
199
- bytes_uploaded = subject.upload(core_dir, dest_dir)
200
- expect(bytes_uploaded).to be > 0
201
- end
202
- end
203
- 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) { __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) { 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 using relative file path' do
61
+ subject.upload('./spec/integration/file_manager_spec.rb', dest_file)
62
+ expect(subject).to have_created(dest_file).with_content(this_file)
63
+ end
64
+
65
+ it 'should upload to the specified directory' do
66
+ subject.create_dir(dest_dir)
67
+ subject.upload(this_file, dest_dir)
68
+ expect(subject).to have_created(dest_file).with_content(this_file)
69
+ end
70
+
71
+ it 'should treat extensionless target as file if not an existing directory' do
72
+ subject.upload(this_file, dest_dir)
73
+ expect(subject).to have_created(dest_dir).with_content(this_file)
74
+ end
75
+
76
+ it 'should create extensionless source under target dir if target dir exists' do
77
+ subject.create_dir(dest_dir)
78
+ src_file = File.expand_path('../../Gemfile', File.dirname(__FILE__))
79
+ subject.upload(src_file, dest_dir)
80
+ expect(subject).to have_created(File.join(dest_dir, 'Gemfile')).with_content(src_file)
81
+ end
82
+
83
+ it 'should upload to the specified directory with env var' do
84
+ subject.upload(this_file, '$env:Temp')
85
+ expected_dest_file = File.join(subject.temp_dir, File.basename(this_file))
86
+ expect(subject).to have_created(expected_dest_file).with_content(this_file)
87
+ end
88
+
89
+ it 'should upload to Program Files sub dir' do
90
+ subject.create_dir('$env:ProgramFiles/foo')
91
+ subject.upload(this_file, '$env:ProgramFiles/foo')
92
+ expect(subject).to have_created('c:/Program Files/foo/file_manager_spec.rb') \
93
+ .with_content(this_file)
94
+ end
95
+
96
+ it 'should upload to the specified nested directory' do
97
+ dest_sub_dir = File.join(dest_dir, 'subdir')
98
+ subject.create_dir(dest_sub_dir)
99
+ dest_sub_dir_file = File.join(dest_sub_dir, File.basename(this_file))
100
+ subject.upload(this_file, dest_sub_dir)
101
+ expect(subject).to have_created(dest_sub_dir_file).with_content(this_file)
102
+ end
103
+
104
+ it 'yields progress data' do
105
+ block_called = false
106
+ total_bytes_copied = 0
107
+ total = subject.upload(this_file, dest_file) do \
108
+ |bytes_copied, total_bytes, local_path, remote_path|
109
+ expect(total_bytes).to be > 0
110
+ total_bytes_copied = bytes_copied
111
+ expect(local_path).to eq(this_file)
112
+ expect(remote_path).to eq(dest_file)
113
+ block_called = true
114
+ end
115
+ expect(total_bytes_copied).to eq(total)
116
+ expect(block_called).to be true
117
+ expect(total).to be > 0
118
+ end
119
+
120
+ it 'should not upload when content matches' do
121
+ subject.upload(this_file, dest_dir)
122
+ bytes_uploaded = subject.upload(this_file, dest_dir)
123
+ expect(bytes_uploaded).to eq 0
124
+ end
125
+
126
+ it 'should upload when content differs' do
127
+ matchers_file = File.join(spec_dir, 'matchers.rb')
128
+ subject.upload(matchers_file, dest_file)
129
+ bytes_uploaded = subject.upload(this_file, dest_file)
130
+ expect(bytes_uploaded).to be > 0
131
+ end
132
+
133
+ it 'raises WinRMUploadError when a bad source path is specified' do
134
+ expect { subject.upload('c:/some/non-existant/path/foo', dest_file) }.to raise_error
135
+ end
136
+ end
137
+
138
+ context 'upload empty file' do
139
+ let(:empty_src_file) { Tempfile.new('empty') }
140
+ let(:dest_file) { File.join(dest_dir, 'emptyfile.txt') }
141
+
142
+ it 'creates a new empty file' do
143
+ expect(subject.upload(empty_src_file.path, dest_file)).to be 0
144
+ expect(subject).to have_created(dest_file).with_content('')
145
+ end
146
+
147
+ it 'overwrites an existing file' do
148
+ expect(subject.upload(this_file, dest_file)).to be > 0
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
+ end
153
+
154
+ context 'upload directory' do
155
+ let(:root_dir) { File.expand_path('../../', File.dirname(__FILE__)) }
156
+ let(:winrm_fs_dir) { File.join(root_dir, 'lib/winrm-fs') }
157
+ let(:core_dir) { File.join(root_dir, 'lib/winrm-fs/core') }
158
+ let(:scripts_dir) { File.join(root_dir, 'lib/winrm-fs/scripts') }
159
+
160
+ it 'copies the directory contents recursively when directory does not exist' do
161
+ bytes_uploaded = subject.upload(winrm_fs_dir, dest_dir)
162
+ expect(bytes_uploaded).to be > 0
163
+
164
+ Dir.glob(winrm_fs_dir + '/**/*.rb').each do |host_file|
165
+ host_file_rel = Pathname.new(host_file).relative_path_from(
166
+ Pathname.new(winrm_fs_dir)
167
+ ).to_s
168
+ remote_file = File.join(dest_dir, host_file_rel)
169
+ expect(subject).to have_created(remote_file).with_content(host_file)
170
+ end
171
+ end
172
+
173
+ it 'copies the directory recursively when directory does exist' do
174
+ subject.create_dir(dest_dir)
175
+ bytes_uploaded = subject.upload(winrm_fs_dir, dest_dir)
176
+ expect(bytes_uploaded).to be > 0
177
+
178
+ Dir.glob(winrm_fs_dir + '/**/*.rb').each do |host_file|
179
+ host_file_rel = Pathname.new(host_file).relative_path_from(
180
+ Pathname.new(winrm_fs_dir).dirname
181
+ ).to_s
182
+ remote_file = File.join(dest_dir, host_file_rel)
183
+ expect(subject).to have_created(remote_file).with_content(host_file)
184
+ end
185
+ end
186
+
187
+ it 'does not copy the directory when content is the same' do
188
+ subject.upload(winrm_fs_dir, dest_dir)
189
+ bytes_uploaded = subject.upload(winrm_fs_dir, dest_dir)
190
+ expect(bytes_uploaded).to eq 0
191
+ end
192
+
193
+ it 'unzips the directory when cached content is the same' do
194
+ subject.upload(winrm_fs_dir, dest_dir)
195
+ subject.delete(dest_dir)
196
+ expect(subject.exists?(dest_dir)).to be false
197
+ subject.upload(winrm_fs_dir, dest_dir)
198
+ expect(subject.exists?(dest_dir)).to be true
199
+ end
200
+
201
+ it 'unzips multiple directories when cached content is the same for all' do
202
+ subject.create_dir(dest_dir)
203
+ subject.upload([core_dir, scripts_dir], dest_dir)
204
+ subject.delete(dest_dir)
205
+ expect(subject.exists?(dest_dir)).to be false
206
+ subject.create_dir(dest_dir)
207
+ subject.upload([core_dir, scripts_dir], dest_dir)
208
+ expect(subject.exists?(File.join(dest_dir, 'core'))).to be true
209
+ expect(subject.exists?(File.join(dest_dir, 'scripts'))).to be true
210
+ end
211
+
212
+ it 'copies the directory when content differs' do
213
+ subject.upload(winrm_fs_dir, dest_dir)
214
+ bytes_uploaded = subject.upload(core_dir, dest_dir)
215
+ expect(bytes_uploaded).to be > 0
216
+ end
217
+ end
218
+ end