adds schema view and schema generating command
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
import Foundation
|
||||
import HelderCore
|
||||
import HelderServices
|
||||
import Testing
|
||||
|
||||
@Suite struct SchemaServiceTests {
|
||||
/// A Laravel-looking project plus a `bin` folder holding a fake `php` with the given shell body.
|
||||
private func project(php body: String? = nil) throws -> (dir: TempDirectory, bin: URL) {
|
||||
let dir = try TempDirectory()
|
||||
try dir.write("project/artisan", "<?php\n")
|
||||
try dir.mkdir("bin")
|
||||
if let body { try script(in: dir, "bin/php", body) }
|
||||
return (dir, dir.url("bin"))
|
||||
}
|
||||
|
||||
private func script(in dir: TempDirectory, _ path: String, _ body: String) throws {
|
||||
try dir.write(path, "#!/bin/sh\n" + body + "\n")
|
||||
try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: dir.url(path).path)
|
||||
}
|
||||
|
||||
private func service(_ dir: TempDirectory, bin: URL, herd: String? = nil, timeout: TimeInterval = 20) -> SchemaService {
|
||||
let php = PHPLocator(herdPath: herd ?? dir.url("no-herd/php").path, locator: ExecutableLocator(directories: [bin.path]))
|
||||
return SchemaService(root: dir.url("project"), php: php, timeout: timeout)
|
||||
}
|
||||
|
||||
// MARK: files
|
||||
|
||||
@Test func detectsArtisanAndTheCommand() async throws {
|
||||
let (dir, bin) = try project()
|
||||
let schema = service(dir, bin: bin)
|
||||
#expect(await schema.isLaravelApp())
|
||||
#expect(!(await schema.hasCommand()))
|
||||
try dir.write("project/" + SchemaPaths.commandFile, "<?php\n")
|
||||
#expect(await schema.hasCommand())
|
||||
try dir.mkdir("other/artisan")
|
||||
#expect(!(await SchemaService(root: dir.url("other")).isLaravelApp()))
|
||||
}
|
||||
|
||||
@Test func aMissingDumpIsNil() async throws {
|
||||
let (dir, bin) = try project()
|
||||
let schema = service(dir, bin: bin)
|
||||
#expect(try await schema.readDump() == nil)
|
||||
#expect(await schema.dumpModificationDate() == nil)
|
||||
}
|
||||
|
||||
@Test func readsTheDumpIntoAModel() async throws {
|
||||
let (dir, bin) = try project()
|
||||
try dir.write("project/" + SchemaPaths.dumpFile, """
|
||||
{"version": 1, "driver": "mysql", "database": "odynt", "tables": [
|
||||
{"name": "users", "rows": 2, "columns": [{"name": "id", "type": "bigint"}], "indexes": [{"columns": ["id"], "primary": true}]},
|
||||
{"name": "posts", "columns": [{"name": "id"}, {"name": "user_id"}],
|
||||
"foreign_keys": [{"columns": ["user_id"], "foreign_table": "users", "foreign_columns": ["id"]}]}
|
||||
]}
|
||||
""")
|
||||
let schema = service(dir, bin: bin)
|
||||
let model = try #require(try await schema.readDump())
|
||||
#expect(model.connectionLabel == "mysql · odynt")
|
||||
#expect(model.tables.map(\.name) == ["posts", "users"])
|
||||
#expect(model.relationCount == 1)
|
||||
let date = try #require(await schema.dumpModificationDate())
|
||||
#expect(abs(date.timeIntervalSinceNow) < 60)
|
||||
}
|
||||
|
||||
@Test func aCorruptDumpThrowsAReadableError() async throws {
|
||||
let (dir, bin) = try project()
|
||||
try dir.write("project/" + SchemaPaths.dumpFile, "{\"tables\": [")
|
||||
await #expect(throws: SchemaDumpError.self) {
|
||||
try await service(dir, bin: bin).readDump()
|
||||
}
|
||||
do {
|
||||
_ = try await service(dir, bin: bin).readDump()
|
||||
} catch {
|
||||
#expect(error.localizedDescription.hasPrefix("database/helder/schema.json is not a valid schema dump"))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: runDump
|
||||
|
||||
@Test func runsArtisanInTheProjectAndStreamsBothStreams() async throws {
|
||||
let (dir, bin) = try project(php: """
|
||||
echo "args: $*"
|
||||
echo "cwd: $(pwd -P)"
|
||||
echo "oops" >&2
|
||||
echo " Wrote database/helder/schema.json (1.0 KB)"
|
||||
""")
|
||||
let log = LineLog()
|
||||
let code = await service(dir, bin: bin).runDump(onLine: log.append)
|
||||
#expect(code == 0)
|
||||
let outputs = log.outputs
|
||||
#expect(outputs.count == 3)
|
||||
#expect(outputs.first == "args: artisan helder:schema --no-ansi --no-interaction")
|
||||
// pwd -P resolves /var to /private/var, so compare the tail only.
|
||||
#expect(outputs.dropFirst().first?.hasSuffix("/" + dir.root.lastPathComponent + "/project") == true)
|
||||
#expect(outputs.last == " Wrote database/helder/schema.json (1.0 KB)")
|
||||
#expect(log.errors == ["oops"])
|
||||
}
|
||||
|
||||
@Test func theExitCodeComesBack() async throws {
|
||||
let (dir, bin) = try project(php: "echo ' ERROR Connection refused.'\nexit 1")
|
||||
let log = LineLog()
|
||||
#expect(await service(dir, bin: bin).runDump(onLine: log.append) == 1)
|
||||
#expect(log.outputs == [" ERROR Connection refused."])
|
||||
}
|
||||
|
||||
@Test func herdsPhpWinsAndItsFolderLeadsThePath() async throws {
|
||||
let (dir, bin) = try project(php: "echo path-php")
|
||||
try script(in: dir, "herd/bin/php", "echo herd\necho \"${PATH%%:*}\"")
|
||||
let log = LineLog()
|
||||
let code = await service(dir, bin: bin, herd: dir.url("herd/bin/php").path).runDump(onLine: log.append)
|
||||
#expect(code == 0)
|
||||
#expect(log.outputs == ["herd", dir.url("herd/bin").path])
|
||||
}
|
||||
|
||||
@Test func thePhpOnThePathIsTheFallback() async throws {
|
||||
let (dir, bin) = try project(php: "echo \"${PATH%%:*}\"")
|
||||
let log = LineLog()
|
||||
#expect(await service(dir, bin: bin).runDump(onLine: log.append) == 0)
|
||||
#expect(log.outputs == [bin.path])
|
||||
}
|
||||
|
||||
@Test func noPhpIsOneClearErrorLine() async throws {
|
||||
let (dir, bin) = try project()
|
||||
let log = LineLog()
|
||||
let code = await service(dir, bin: bin).runDump(onLine: log.append)
|
||||
#expect(code != 0)
|
||||
#expect(log.errors == ["php not found. Install Laravel Herd or put php on PATH."])
|
||||
#expect(log.outputs.isEmpty)
|
||||
}
|
||||
|
||||
@Test func theRunNeverWaitsOnInput() async throws {
|
||||
let (dir, bin) = try project(php: "read answer\necho \"read: [$answer]\"")
|
||||
let log = LineLog()
|
||||
#expect(await service(dir, bin: bin, timeout: 10).runDump(onLine: log.append) == 0)
|
||||
#expect(log.outputs == ["read: []"])
|
||||
}
|
||||
|
||||
@Test func aHungRunTimesOut() async throws {
|
||||
let (dir, bin) = try project(php: "echo started\nexec sleep 30")
|
||||
let log = LineLog()
|
||||
let start = Date()
|
||||
let code = await service(dir, bin: bin, timeout: 0.5).runDump(onLine: log.append)
|
||||
#expect(code == -1)
|
||||
#expect(log.errors == ["timed out after 0.5s"])
|
||||
#expect(Date().timeIntervalSince(start) < 10)
|
||||
}
|
||||
}
|
||||
|
||||
@Suite struct SchemaLayoutStoreTests {
|
||||
private func withDefaults(_ body: (UserDefaults) throws -> Void) rethrows {
|
||||
let suite = "helder-test-\(UUID().uuidString)"
|
||||
let defaults = UserDefaults(suiteName: suite)!
|
||||
defer { defaults.removePersistentDomain(forName: suite) }
|
||||
try body(defaults)
|
||||
}
|
||||
|
||||
@Test func positionsRoundTripPerProject() {
|
||||
withDefaults { defaults in
|
||||
let store = UserDefaultsSchemaLayoutStore(defaults: defaults)
|
||||
#expect(store.loadPositions(root: "/a").isEmpty)
|
||||
store.savePositions(["users": SchemaPoint(x: 40, y: 104.5)], root: "/a")
|
||||
#expect(store.loadPositions(root: "/a") == ["users": SchemaPoint(x: 40, y: 104.5)])
|
||||
#expect(store.loadPositions(root: "/b").isEmpty)
|
||||
#expect(defaults.data(forKey: "helder.schemaLayout:/a") != nil)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func unreadablePositionsAreEmpty() {
|
||||
withDefaults { defaults in
|
||||
defaults.set(Data("nope".utf8), forKey: SchemaLayout.defaultsKey(root: "/a"))
|
||||
#expect(UserDefaultsSchemaLayoutStore(defaults: defaults).loadPositions(root: "/a").isEmpty)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user