@@ -27,7 +27,7 @@ Describe 'Add-GoogleDriveFile' {
2727
2828 # folder does not exist
2929 Mock Get-GoogleDriveItems - ParameterFilter {
30- $ResultType -eq " Folders" -and $Title -eq " TestFolder "
30+ $ResultType -eq " Folders"
3131 } { return $null }
3232
3333 # folder was added
@@ -55,7 +55,7 @@ Describe 'Add-GoogleDriveFile' {
5555 InModuleScope PSBlogger {
5656 # folder exists
5757 Mock Get-GoogleDriveItems - ParameterFilter {
58- $ResultType -eq " Folders" -and $Title -eq " TestFolder "
58+ $ResultType -eq " Folders"
5959 } { return @ ([pscustomobject ]@ { id = " 12345" ; name = " TestFolder" }) }
6060 }
6161 }
@@ -144,6 +144,91 @@ Describe 'Add-GoogleDriveFile' {
144144 Should - InvokeVerifiable
145145 }
146146 }
147+
148+ Context " Preserve subfolder path" {
149+
150+ It " Should create full paths in GoogleDrive matching the supplied FileName" {
151+ # arrange
152+ InModuleScope PSBlogger {
153+ # folders do not exist
154+ Mock Get-GoogleDriveItems - ParameterFilter {
155+ $ResultType -eq " Folders"
156+ } { return $null }
157+
158+ # create root folder
159+ Mock Add-GoogleDriveFolder - ParameterFilter { $Name -eq " TestFolder" } {
160+ return [pscustomobject ]@ { id = " 12345" ; name = " TestFolder" }
161+ } - Verifiable
162+
163+ # create subfolder
164+ Mock Add-GoogleDriveFolder - ParameterFilter { $Name -eq " subfolder" -and $ParentId -eq " 12345" } {
165+ return [pscustomobject ]@ { id = " 67890" ; name = " subfolder" }
166+ } - Verifiable
167+
168+ # file in subfolder does not exist
169+ Mock Get-GoogleDriveItems - ParameterFilter {
170+ $ResultType -eq " Files" -and $Title -eq " my.png" -and $ParentId -eq " 67890"
171+ } { return $null }
172+
173+ # file was added
174+ Mock Invoke-GApi - ParameterFilter {
175+ $uri -eq " https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart" -and `
176+ $body -match ' "parents":\s*\["67890"\]' -and `
177+ $body -match ' "name":\s*"my\.png"'
178+ } {
179+ return [pscustomobject ]@ { id = " abcde" ; name = " my.png" }
180+ } - Verifiable
181+ }
182+
183+ # act
184+ $result = Add-GoogleDriveFile - FilePath " $PSScriptRoot \data\my.png" - FileName " subfolder/my.png" - TargetFolderName " TestFolder"
185+
186+ # assert
187+ $result | Should -Not - BeNullOrEmpty
188+ Should - InvokeVerifiable
189+ }
190+
191+ It " Should create missing subfolders in Google Drive matching the supplied FileName" {
192+ # arrange
193+ InModuleScope PSBlogger {
194+ # folders do not exist
195+ Mock Get-GoogleDriveItems - ParameterFilter {
196+ $ResultType -eq " Folders"
197+ } {
198+ @ (
199+ [pscustomobject ]@ { id = " 12345" ; name = " TestFolder" }
200+ )
201+ }
202+
203+ # create subfolder
204+ Mock Add-GoogleDriveFolder - ParameterFilter { $Name -eq " subfolder" -and $ParentId -eq " 12345" } {
205+ return [pscustomobject ]@ { id = " 67890" ; name = " subfolder" }
206+ } - Verifiable
207+
208+ # file in subfolder does not exist
209+ Mock Get-GoogleDriveItems - ParameterFilter {
210+ $ResultType -eq " Files" -and $Title -eq " my.png" -and $ParentId -eq " 67890"
211+ } { return $null }
212+
213+ # file was added
214+ Mock Invoke-GApi - ParameterFilter {
215+ $uri -eq " https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart" -and `
216+ $body -match ' "parents":\s*\["67890"\]' -and `
217+ $body -match ' "name":\s*"my\.png"'
218+ } {
219+ return [pscustomobject ]@ { id = " abcde" ; name = " my.png" }
220+ } - Verifiable
221+ }
222+
223+ # act
224+ $result = Add-GoogleDriveFile - FilePath " $PSScriptRoot \data\my.png" - FileName " subfolder/my.png" - TargetFolderName " TestFolder"
225+
226+ # assert
227+ $result | Should -Not - BeNullOrEmpty
228+ Should - InvokeVerifiable
229+
230+ }
231+ }
147232 }
148233
149234 Context " Existing File" {
@@ -152,7 +237,7 @@ Describe 'Add-GoogleDriveFile' {
152237 InModuleScope PSBlogger {
153238 # folder exists
154239 Mock Get-GoogleDriveItems - ParameterFilter {
155- $ResultType -eq " Folders" -and $Title -eq " TestFolder "
240+ $ResultType -eq " Folders"
156241 } { return @ ([pscustomobject ]@ { id = " 12345" ; name = " TestFolder" }) }
157242
158243 # file exists
0 commit comments