mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2026-04-01 01:00:08 +02:00
♻️ Refactor tests to Swift Testing
This commit is contained in:
139
tests/unit/Testables/Filesystem/RealFileSystemTest.swift
Normal file
139
tests/unit/Testables/Filesystem/RealFileSystemTest.swift
Normal file
@@ -0,0 +1,139 @@
|
||||
//
|
||||
// RealFileSystemTest.swift
|
||||
// Unit Tests
|
||||
//
|
||||
// Created by Nico Verbruggen on 02/11/2022.
|
||||
// Copyright © 2023 Nico Verbruggen. All rights reserved.
|
||||
//
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
|
||||
@Suite(.serialized)
|
||||
struct RealFileSystemTest {
|
||||
var filesystem: FileSystemProtocol
|
||||
|
||||
init() throws {
|
||||
let container = Container()
|
||||
container.prepare()
|
||||
|
||||
filesystem = container.filesystem
|
||||
}
|
||||
|
||||
private func createUniqueTemporaryDirectory() -> String {
|
||||
let tempDirectoryURL = NSURL.fileURL(withPath: NSTemporaryDirectory(), isDirectory: true)
|
||||
let fullTempDirectoryPath = tempDirectoryURL.appendingPathComponent("phpmon-fs-tests").path
|
||||
try? FileManager.default.removeItem(atPath: fullTempDirectoryPath)
|
||||
try! FileManager.default.createDirectory(atPath: fullTempDirectoryPath, withIntermediateDirectories: false)
|
||||
return fullTempDirectoryPath
|
||||
}
|
||||
|
||||
private func createTestBinaryFile(_ temporaryDirectory: String) -> String {
|
||||
let executablePath = "\(temporaryDirectory)/exec.sh"
|
||||
|
||||
try! filesystem.writeAtomicallyToFile(executablePath, content: """
|
||||
!#/bin/bash
|
||||
echo 'Hello world';
|
||||
""")
|
||||
|
||||
return executablePath
|
||||
}
|
||||
|
||||
@Test func testable_fs_is_in_use() {
|
||||
#expect(filesystem is RealFileSystem)
|
||||
}
|
||||
|
||||
@Test func temporary_path_exists() {
|
||||
let temporaryDirectory = self.createUniqueTemporaryDirectory()
|
||||
|
||||
// True
|
||||
#expect(filesystem.directoryExists(temporaryDirectory))
|
||||
#expect(filesystem.anyExists(temporaryDirectory))
|
||||
|
||||
// False
|
||||
#expect(!filesystem.fileExists(temporaryDirectory))
|
||||
}
|
||||
|
||||
@Test func directory_can_be_created_symlinked_and_read() {
|
||||
let temporaryDirectory = self.createUniqueTemporaryDirectory()
|
||||
|
||||
let folderPath = "\(temporaryDirectory)/brew/etc/lib/c"
|
||||
|
||||
try! filesystem.createDirectory(folderPath, withIntermediateDirectories: true)
|
||||
|
||||
#expect(filesystem.directoryExists("\(temporaryDirectory)/brew"))
|
||||
#expect(filesystem.directoryExists("\(temporaryDirectory)/brew/etc"))
|
||||
#expect(filesystem.directoryExists("\(temporaryDirectory)/brew/etc/lib"))
|
||||
#expect(filesystem.directoryExists("\(temporaryDirectory)/brew/etc/lib/c"))
|
||||
|
||||
_ = system("ln -s \(temporaryDirectory)/brew/etc/lib/c \(temporaryDirectory)/c")
|
||||
#expect(filesystem.directoryExists("\(temporaryDirectory)/c"))
|
||||
#expect(filesystem.isSymlink("\(temporaryDirectory)/c"))
|
||||
#expect(
|
||||
try! filesystem.getDestinationOfSymlink("\(temporaryDirectory)/c") ==
|
||||
"\(temporaryDirectory)/brew/etc/lib/c"
|
||||
)
|
||||
|
||||
let contents = try! filesystem.getShallowContentsOfDirectory("\(temporaryDirectory)/brew/etc/lib/c")
|
||||
#expect([] == contents)
|
||||
}
|
||||
|
||||
@Test func can_read_file_as_text() {
|
||||
let temporaryDirectory = self.createUniqueTemporaryDirectory()
|
||||
let executable = self.createTestBinaryFile(temporaryDirectory)
|
||||
|
||||
#expect(
|
||||
try! filesystem.getStringFromFile(executable) ==
|
||||
"""
|
||||
!#/bin/bash
|
||||
echo 'Hello world';
|
||||
"""
|
||||
)
|
||||
}
|
||||
|
||||
@Test func make_binary_executable() {
|
||||
let temporaryDirectory = self.createUniqueTemporaryDirectory()
|
||||
let executable = self.createTestBinaryFile(temporaryDirectory)
|
||||
|
||||
#expect(filesystem.isWriteableFile(executable))
|
||||
#expect(!filesystem.isExecutableFile(executable))
|
||||
|
||||
try! filesystem.makeExecutable(executable)
|
||||
|
||||
#expect(filesystem.isExecutableFile(executable))
|
||||
#expect(!filesystem.isDirectory(executable))
|
||||
#expect(!filesystem.isSymlink(executable))
|
||||
}
|
||||
|
||||
@Test func non_existent_file_is_not_symlink_or_directory() {
|
||||
let path = "/path/that/does/not/exist"
|
||||
|
||||
#expect(!filesystem.isDirectory(path))
|
||||
#expect(!filesystem.isSymlink(path))
|
||||
}
|
||||
|
||||
@Test func moving_file() {
|
||||
let temporaryDirectory = self.createUniqueTemporaryDirectory()
|
||||
let executable = self.createTestBinaryFile(temporaryDirectory)
|
||||
|
||||
#expect(filesystem.fileExists(executable))
|
||||
|
||||
let newExecutable = executable.replacingOccurrences(of: "/exec.sh", with: "/file.txt")
|
||||
|
||||
try! filesystem.move(from: executable, to: newExecutable)
|
||||
|
||||
#expect(filesystem.fileExists(newExecutable))
|
||||
#expect(!filesystem.fileExists(executable))
|
||||
}
|
||||
|
||||
@Test func deleting_file() {
|
||||
let temporaryDirectory = self.createUniqueTemporaryDirectory()
|
||||
let executable = self.createTestBinaryFile(temporaryDirectory)
|
||||
|
||||
#expect(filesystem.fileExists(executable))
|
||||
|
||||
try! filesystem.remove(executable)
|
||||
|
||||
#expect(!filesystem.fileExists(executable))
|
||||
}
|
||||
}
|
||||
122
tests/unit/Testables/Filesystem/TestableFileSystemTest.swift
Normal file
122
tests/unit/Testables/Filesystem/TestableFileSystemTest.swift
Normal file
@@ -0,0 +1,122 @@
|
||||
//
|
||||
// TestableFileSystemTest.swift
|
||||
// PHP Monitor
|
||||
//
|
||||
// Created by Nico Verbruggen on 01/11/2022.
|
||||
// Copyright © 2023 Nico Verbruggen. All rights reserved.
|
||||
//
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
|
||||
@Suite(.serialized)
|
||||
struct TestableFileSystemTest {
|
||||
var container: Container
|
||||
init() throws {
|
||||
container = Container.fake(files: [
|
||||
"/home/user/bin/foo": .fake(.binary),
|
||||
"/home/user/docs": .fake(.symlink, "/home/user/documents"),
|
||||
"/home/user/documents/script.sh": .fake(.text, "echo 'cool';"),
|
||||
"/home/user/documents/nice.txt": .fake(.text, "69"),
|
||||
"/home/user/documents/filters/filter1.txt": .fake(.text, "F1"),
|
||||
"/home/user/documents/filters/filter2.txt": .fake(.text, "F2")
|
||||
])
|
||||
}
|
||||
|
||||
var FileSystem: FileSystemProtocol {
|
||||
return container.filesystem
|
||||
}
|
||||
|
||||
@Test func testable_fs_is_in_use() {
|
||||
#expect(FileSystem is TestableFileSystem)
|
||||
}
|
||||
|
||||
@Test func intermediate_directories_are_automatically_created() {
|
||||
#expect(FileSystem.directoryExists("/"))
|
||||
#expect(FileSystem.directoryExists("/home"))
|
||||
#expect(FileSystem.directoryExists("/home/user"))
|
||||
#expect(FileSystem.directoryExists("/home/user/documents"))
|
||||
#expect(FileSystem.directoryExists("/home/user/bin"))
|
||||
}
|
||||
|
||||
@Test func binary_directory_is_writable() {
|
||||
#expect(FileSystem.isWriteableFile("/home/user/bin"))
|
||||
}
|
||||
|
||||
@Test func binary_exists() {
|
||||
#expect(FileSystem.isExecutableFile("/home/user/bin/foo"))
|
||||
}
|
||||
|
||||
@Test func can_write_text_to_executable() throws {
|
||||
try! FileSystem.writeAtomicallyToFile("/home/user/bin/bar", content: "bar bar bar!")
|
||||
|
||||
#expect(FileSystem.fileExists("/home/user/bin/bar"))
|
||||
#expect(!FileSystem.isExecutableFile("/home/user/bin/bar"))
|
||||
|
||||
try! FileSystem.makeExecutable("/home/user/bin/bar")
|
||||
|
||||
#expect(FileSystem.isExecutableFile("/home/user/bin/bar"))
|
||||
}
|
||||
|
||||
@Test func can_create_directory() throws {
|
||||
try! FileSystem.createDirectory(
|
||||
"/home/nico/phpmon/config",
|
||||
withIntermediateDirectories: true
|
||||
)
|
||||
|
||||
#expect(FileSystem
|
||||
.anyExists("/home/nico/phpmon/config"))
|
||||
#expect(FileSystem.directoryExists("/home/nico/phpmon/config"))
|
||||
}
|
||||
|
||||
@Test func can_create_nested_directories() throws {
|
||||
try FileSystem.createDirectory(
|
||||
"/home/user/thing/epic/nested/directories",
|
||||
withIntermediateDirectories: true
|
||||
)
|
||||
|
||||
#expect(FileSystem.directoryExists("/"))
|
||||
#expect(FileSystem.directoryExists("/home"))
|
||||
#expect(FileSystem.directoryExists("/home/user"))
|
||||
#expect(FileSystem.directoryExists("/home/user/thing"))
|
||||
#expect(FileSystem.directoryExists("/home/user/thing/epic/nested"))
|
||||
#expect(FileSystem.directoryExists("/home/user/thing/epic/nested/directories"))
|
||||
}
|
||||
|
||||
@Test func can_list_directory_contents() throws {
|
||||
let contents = try! FileSystem.getShallowContentsOfDirectory("/home/user/documents")
|
||||
|
||||
#expect(
|
||||
contents.sorted() ==
|
||||
[
|
||||
"script.sh",
|
||||
"nice.txt",
|
||||
"filters"
|
||||
].sorted()
|
||||
)
|
||||
}
|
||||
|
||||
@Test func can_delete_directory_recursively() {
|
||||
#expect(FileSystem.directoryExists("/home/user/documents"))
|
||||
#expect(FileSystem.directoryExists("/home/user/documents/filters"))
|
||||
#expect(FileSystem.fileExists("/home/user/documents/filters/filter1.txt"))
|
||||
|
||||
try! FileSystem.remove("/home/user/documents")
|
||||
|
||||
#expect(!FileSystem.directoryExists("/home/user/documents"))
|
||||
#expect(!FileSystem.directoryExists("/home/user/documents/filters"))
|
||||
#expect(!FileSystem.fileExists("/home/user/documents/filters/filter1.txt"))
|
||||
}
|
||||
|
||||
@Test func can_move_directory() {
|
||||
#expect(FileSystem.directoryExists("/home/user/documents"))
|
||||
#expect(FileSystem.directoryExists("/home/user/documents/filters"))
|
||||
#expect(FileSystem.fileExists("/home/user/documents/filters/filter1.txt"))
|
||||
|
||||
try! FileSystem.move(from: "/home/user/documents", to: "/home/user/new")
|
||||
|
||||
#expect(FileSystem.directoryExists("/home/user/new"))
|
||||
#expect(FileSystem.directoryExists("/home/user/new/filters"))
|
||||
#expect(FileSystem.fileExists("/home/user/new/filters/filter1.txt"))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user