From d7a06b062aa9e2b66fde94e3f7a96757591df9aa Mon Sep 17 00:00:00 2001 From: Hsiao-nan Cheung Date: Thu, 16 May 2019 09:54:57 +0800 Subject: [PATCH] Update Scoop-Test testcases --- test/Scoop-Core.Tests.ps1 | 43 +++++- test/Scoop-Install.Tests.ps1 | 33 +---- test/Scoop-Persist.Tests.ps1 | 161 ++++++++++++++++++++++ test/fixtures/persist/persist-array.json | 35 +++++ test/fixtures/persist/persist-object.json | 61 ++++++++ 5 files changed, 298 insertions(+), 35 deletions(-) create mode 100644 test/Scoop-Persist.Tests.ps1 create mode 100644 test/fixtures/persist/persist-array.json create mode 100644 test/fixtures/persist/persist-object.json diff --git a/test/Scoop-Core.Tests.ps1 b/test/Scoop-Core.Tests.ps1 index 04bab9c478..2b2183072a 100644 --- a/test/Scoop-Core.Tests.ps1 +++ b/test/Scoop-Core.Tests.ps1 @@ -111,21 +111,58 @@ describe "Test-CommandAvailable" -Tag 'Scoop' { } -describe "is_directory" -Tag 'Scoop' { +describe "directory/junction/hardlink handling" -Tag 'Scoop' { beforeall { $working_dir = setup_working "is_directory" } - it "is_directory recognize directories" { + it "is_directory recognizes directories" { is_directory "$working_dir\i_am_a_directory" | should -be $true } - it "is_directory recognize files" { + + it "is_directory recognizes files" { is_directory "$working_dir\i_am_a_file.txt" | should -be $false } it "is_directory is falsey on unknown path" { is_directory "$working_dir\i_do_not_exist" | should -be $false } + + it "create_junction recognizes directories and creates junction" { + create_junction "$working_dir\i_am_a_junction" "$working_dir\i_am_a_directory" | should -be $true + Test-Path "$working_dir\i_am_a_junction" | should -be $true + } + + it "create_junction recognizes existing target" { + create_junction "$working_dir\i_am_a_junction" "$working_dir\i_am_a_directory" | should -be $false + } + + it "create_junction recognizes files" { + create_junction "$working_dir\i_am_a_junction_file.txt" "$working_dir\i_am_a_file.txt" | should -be $false + Test-Path "$working_dir\i_am_a_junction_file.txt" | should -be $false + } + + it "create_junction is falsey on unknown path" { + create_junction "$working_dir\i_am_a_junction" "$working_dir\i_do_not_exist" | should -be $false + } + + it "create_hardlink recognizes files and creates hardlink" { + create_hardlink "$working_dir\i_am_a_hardlinked_file.txt" "$working_dir\i_am_a_file.txt" | should -be $true + Test-Path "$working_dir\i_am_a_hardlinked_file.txt" | should -be $true + } + + it "create_hardlink recognizes existing target" { + create_hardlink "$working_dir\i_am_a_hardlinked_file.txt" "$working_dir\i_am_a_file.txt" | should -be $false + } + + it "create_hardlink recognizes directories" { + create_hardlink "$working_dir\i_am_a_hardlinked_directory" "$working_dir\i_am_a_directory" | should -be $false + Test-Path "$working_dir\i_am_a_hardlinked_directory" | should -be $false + } + + it "create_hardlink is falsey on unknown path" { + create_hardlink "$working_dir\i_do_not_exist.txt" "$working_dir\i_do_not_exist.txt" | should -be $false + } } describe "movedir" -Tag 'Scoop' { diff --git a/test/Scoop-Install.Tests.ps1 b/test/Scoop-Install.Tests.ps1 index f73c44baa0..40eb869c8c 100644 --- a/test/Scoop-Install.Tests.ps1 +++ b/test/Scoop-Install.Tests.ps1 @@ -101,7 +101,7 @@ describe "env add and remove path" -Tag 'Scoop' { } } -describe "shim_def" -Tag 'Scoop' { +describe "shim parsing" -Tag 'Scoop' { it "should use strings correctly" { $target, $name, $shimArgs = shim_def "command.exe" $target | should -be "command.exe" @@ -122,37 +122,6 @@ describe "shim_def" -Tag 'Scoop' { } } -describe 'persist_def' -Tag 'Scoop' { - it 'parses string correctly' { - $source, $target = persist_def "test" - $source | should -be "test" - $target | should -be "test" - } - - it 'should handle sub-folder' { - $source, $target = persist_def "foo/bar" - $source | should -be "foo/bar" - $target | should -be "foo/bar" - } - - it 'should handle arrays' { - # both specified - $source, $target = persist_def @("foo", "bar") - $source | should -be "foo" - $target | should -be "bar" - - # only first specified - $source, $target = persist_def @("foo") - $source | should -be "foo" - $target | should -be "foo" - - # null value specified - $source, $target = persist_def @("foo", $null) - $source | should -be "foo" - $target | should -be "foo" - } -} - describe 'compute_hash' -Tag 'Scoop' { beforeall { $working_dir = setup_working "manifest" diff --git a/test/Scoop-Persist.Tests.ps1 b/test/Scoop-Persist.Tests.ps1 new file mode 100644 index 0000000000..6d5fc47179 --- /dev/null +++ b/test/Scoop-Persist.Tests.ps1 @@ -0,0 +1,161 @@ +. "$psscriptroot\..\lib\core.ps1" +. "$psscriptroot\..\lib\persist.ps1" +. "$psscriptroot\Scoop-TestLib.ps1" + +describe 'persist parsing' -Tag 'Scoop' { + beforeall { + $working_dir = setup_working "persist" + } + + context "parsing object" { + $objfile = "$working_dir\persist-object.json" + $objfile | should -exist + $obj = Get-Content $objfile -Raw -Encoding UTF8 | ConvertFrom-Json -ea Stop + + it 'should handle directory' { + # explicitly defined + $persist_def = persist_def_obj $obj.persist_dir[0] + $persist_def.source | should -be "foo" + $persist_def.target | should -be "foo" + $persist_def.content | should -be $null + + # implicitly defined + $persist_def = persist_def_obj $obj.persist_dir[1] + $persist_def.source | should -be "foo" + $persist_def.target | should -be "foo" + $persist_def.content | should -be $null + + # sub-dir + $persist_def = persist_def_obj $obj.persist_dir[2] + $persist_def.source | should -be "foo\bar" + $persist_def.target | should -be "foo\bar" + $persist_def.content | should -be $null + + # renaming + $persist_def = persist_def_obj $obj.persist_dir[3] + $persist_def.source | should -be "foo" + $persist_def.target | should -be "bar" + $persist_def.content | should -be $null + + # ignore other params if dir + $persist_def = persist_def_obj $obj.persist_dir[4] + $persist_def.source | should -be "foo" + $persist_def.target | should -be "foo" + $persist_def.encoding | should -be $null + $persist_def.content | should -be $null + + # passthru $method + $persist_def = persist_def_obj $obj.persist_dir[5] + $persist_def.source | should -be "foo" + $persist_def.target | should -be "foo" + $persist_def.content | should -be $null + $persist_def.method | should -be "merge" + } + + it 'should handle file' { + # explicitly defined + $persist_def = persist_def_obj $obj.persist_file[0] + $persist_def.source | should -be "foo" + $persist_def.target | should -be "foo" + $persist_def.content | should -be "" + + # implicitly defined + $persist_def = persist_def_obj $obj.persist_file[1] + $persist_def.source | should -be "foo" + $persist_def.target | should -be "foo" + $persist_def.content | should -be "" + + # passthru file comtents + $persist_def = persist_def_obj $obj.persist_file[2] + $persist_def.source | should -be "foo" + $persist_def.target | should -be "foo" + $persist_def.content | should -be "file content" + + # passthru array file content + $persist_def = persist_def_obj $obj.persist_file[3] + $persist_def.source | should -be "foo" + $persist_def.target | should -be "foo" + $persist_def.content | should -be "file`r`ncontent" + + # using $glue to join array file content + $persist_def = persist_def_obj $obj.persist_file[4] + $persist_def.source | should -be "foo" + $persist_def.target | should -be "foo" + $persist_def.content | should -be "file content" + + # decoding BASE64 string + $persist_def = persist_def_obj $obj.persist_file[5] + $persist_def.source | should -be "foo" + $persist_def.target | should -be "foo" + $persist_def.content | should -be "file`r`ncontent" + + # passthru other params + $persist_def = persist_def_obj $obj.persist_file[6] + $persist_def.source | should -be "foo" + $persist_def.target | should -be "foo" + $persist_def.content | should -be "file`r`ncontent" + $persist_def.method | should -be "update" + $persist_def.encoding | should -be "UTF8" + } + } + + context 'parsing string and array of string' { + $arrfile = "$working_dir\persist-array.json" + $arrfile | should -exist + $arr = Get-Content $arrfile -Raw -Encoding UTF8 | ConvertFrom-Json -ea Stop + + + it 'should handle directory' { + # parse string + $persist_def = persist_def_arr $arr.persist_dir[0] + $persist_def.source | should -be "foo" + $persist_def.target | should -be "foo" + $persist_def.content | should -be $null + + # sub-folder + $persist_def = persist_def_arr $arr.persist_dir[1] + $persist_def.source | should -be "foo\bar" + $persist_def.target | should -be "foo\bar" + $persist_def.content | should -be $null + + # both specified + $persist_def = persist_def_arr $arr.persist_dir[2] + $persist_def.source | should -be "foo" + $persist_def.target | should -be "bar" + $persist_def.content | should -be $null + + # null value specified + $persist_def = persist_def_arr $arr.persist_dir[3] + $persist_def.source | should -be "foo" + $persist_def.target | should -be "foo" + $persist_def.content | should -be $null + } + + it 'should handle file' { + + # no file content specified + $persist_def = persist_def_arr $arr.persist_file[0] + $persist_def.source | should -be "foo" + $persist_def.target | should -be "foo" + $persist_def.content | should -be "" + + # file content specified + $persist_def = persist_def_arr $arr.persist_file[1] + $persist_def.source | should -be "foo" + $persist_def.target | should -be "foo" + $persist_def.content | should -be "file content" + + # null and file content specified + $persist_def = persist_def_arr $arr.persist_file[2] + $persist_def.source | should -be "foo" + $persist_def.target | should -be "foo" + $persist_def.content | should -be "file content" + + # several lines of file content specified + $persist_def = persist_def_arr $arr.persist_file[3] + $persist_def.source | should -be "foo" + $persist_def.target | should -be "foo" + $persist_def.content | should -be "file`r`ncontent" + } + } +} diff --git a/test/fixtures/persist/persist-array.json b/test/fixtures/persist/persist-array.json new file mode 100644 index 0000000000..508b8e0162 --- /dev/null +++ b/test/fixtures/persist/persist-array.json @@ -0,0 +1,35 @@ +{ + "persist_dir": [ + "foo", + "foo\\bar", + [ + "foo", + "bar" + ], + [ + "foo", + null + ] + ], + "persist_file": [ + [ + "foo" + ], + [ + "foo", + "", + "file content" + ], + [ + "foo", + null, + "file content" + ], + [ + "foo", + "", + "file", + "content" + ] + ] +} diff --git a/test/fixtures/persist/persist-object.json b/test/fixtures/persist/persist-object.json new file mode 100644 index 0000000000..8af38f0823 --- /dev/null +++ b/test/fixtures/persist/persist-object.json @@ -0,0 +1,61 @@ +{ + "persist_dir": [ + { + "name": "foo", + "type": "directory" + }, { + "name": "foo\\" + }, { + "name": "foo\\bar\\" + }, { + "name": "foo\\", + "target": "bar" + }, { + "name": "foo\\", + "encoding": "UTF8", + "content": "file content" + }, { + "name": "foo\\", + "method": "merge" + } + ], + "persist_file": [ + { + "name": "foo", + "type": "file" + }, { + "name": "foo" + }, { + "name": "foo", + "content": "file content" + }, { + "name": "foo", + "content": [ + "file", + "content" + ] + }, { + "name": "foo", + "content": [ + "file", + "content" + ], + "glue": " " + }, { + "name": "foo", + "content": [ + "ZmlsZQ0K", + "Y29udGVudA==" + ], + "base64": true + }, { + "name": "foo", + "content": [ + "file", + "content" + ], + "encoding": "UTF8", + "method": "update" + } + ] +}