|
| 1 | +Class UnitTests.FileBinaryTar Extends %UnitTest.TestCase |
| 2 | +{ |
| 3 | + |
| 4 | +/// Path to examples files |
| 5 | +Property Path As %String; |
| 6 | + |
| 7 | +Method OnBeforeAllTests() As %Status |
| 8 | +{ |
| 9 | + set ..Path = ##class(%File).NormalizeDirectory("data", ##class(%File).ParentDirectoryName(^UnitTestRoot)) |
| 10 | + Quit $$$OK |
| 11 | +} |
| 12 | + |
| 13 | +/// Testing extract archive |
| 14 | +Method TestExtract() |
| 15 | +{ |
| 16 | + set fileName = ##class(%File).NormalizeFilename("test.tgz", ##class(%File).ParentDirectoryName(..Path)) |
| 17 | + set extracted = ##class(%zUtils.FileBinaryTar).ExtractFile(fileName) |
| 18 | + do ..CheckExtracted(extracted) |
| 19 | +} |
| 20 | + |
| 21 | +Method CompareFolders(pOriginal As %String, pNew As %String) |
| 22 | +{ |
| 23 | + Set tRS = ##class(%File).FileSetFunc(pOriginal) |
| 24 | + While tRS.%Next() { |
| 25 | + If tRS.Type="D" { |
| 26 | + Do ..CompareFolders(tRS.Name, ##class(%File).NormalizeDirectory(tRS.ItemName, pNew)) |
| 27 | + } |
| 28 | + ElseIf tRS.Type="F" { |
| 29 | + set tName = tRS.ItemName |
| 30 | + If ($LISTFIND($LISTBUILD("tar", "tgz"), $$$lcase($PIECE(tName, ".", *)))) { |
| 31 | + CONTINUE |
| 32 | + } |
| 33 | + set tNewName = ##class(%File).NormalizeDirectory(pNew) _ tName |
| 34 | + if $$$AssertTrue(##class(%File).Exists(tNewName), tNewName) { |
| 35 | + do $$$AssertEquals(##class(%File).GetFileSize(tNewName), ##class(%File).GetFileSize(tRS.Name)) |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +Method CheckExtracted(extracted As %zUtils.FileBinaryTar) [ Internal ] |
| 42 | +{ |
| 43 | + do $$$AssertTrue($isobject(extracted), "Extract successful") |
| 44 | + do $$$AssertStatusOK(extracted.FindPath("folder", .folder), "root folder") |
| 45 | + do $$$AssertStatusOK(extracted.FindPath("folder/subfolder/test.txt", .file), "test.txt in the subfolder") |
| 46 | + if $$$AssertEquals(file.fileData.Read(), "test file in subfolder", "right content") { |
| 47 | + do $$$AssertStatusOK(folder.FindPath("test.txt", .file), "test.txt in the folder") } |
| 48 | + do $$$AssertEquals(file.fileData.Read(), "test file in folder", "right content") |
| 49 | + do $$$AssertStatusOK(folder.FindPath("file with spaces.txt", .file), "file with spaces.txt in the folder") |
| 50 | + do $$$AssertEquals(file.fileData.Read(), "test content for file with spaces", "right content") |
| 51 | + if $$$AssertStatusOK(extracted.FindPath("folder with spaces/test.txt", .file), "test.txt in the folder with spaces") { |
| 52 | + do $$$AssertEquals(file.fileData.Read(), "test file in folder with spaces", "right content") } |
| 53 | + if $$$AssertStatusOK(extracted.FindPath("folder with spaces/file with spaces.txt", .file), "file with spaces.txt in the folder with spaces") { |
| 54 | + do $$$AssertEquals(file.fileData.Read(), "test content for file with spaces", "right content") } |
| 55 | + |
| 56 | + set tmpDir = $$$FileTempDir |
| 57 | + do $$$AssertStatusOK(extracted.ExtractTo(tmpDir)) |
| 58 | + |
| 59 | + Do ..CompareFolders(..Path, tmpDir) |
| 60 | + |
| 61 | + do ##class(%File).RemoveDirectoryTree(tmpDir) |
| 62 | +} |
| 63 | + |
| 64 | +/// Testing Compact some data to archive |
| 65 | +Method TestCompact() |
| 66 | +{ |
| 67 | + set path = ##class(%File).NormalizeDirectory(..Path) |
| 68 | + set tmpFile = ##class(%File).TempFilename("tgz") |
| 69 | + set archive = ##class(%zUtils.FileBinaryTar).Compact(path, 0, tmpFile, path) |
| 70 | + do $$$AssertTrue(##class(%File).Exists(tmpFile), "New archive created") |
| 71 | + |
| 72 | + do archive.Rewind() |
| 73 | + set extracted = ##class(%zUtils.FileBinaryTar).ExtractStream(archive) |
| 74 | + do ..CheckExtracted(extracted) |
| 75 | + #; Do ##class(%File).Delete(tmpFile) |
| 76 | + zw tmpFile |
| 77 | +} |
| 78 | + |
| 79 | +} |
0 commit comments