Fix temporary directory creation to avoid failing pkl-core tests (#28)

This commit is contained in:
Josh B 2024-12-04 08:54:32 -08:00 committed by GitHub
parent 44a04408df
commit 1d0bf43b4b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 17 additions and 18 deletions

View File

@ -85,3 +85,12 @@ public func resolvePaths(_ paths: String...) -> String {
} }
public let absoluteUriRegex = try! Regex("\\w+:") public let absoluteUriRegex = try! Regex("\\w+:")
public func tempDir() throws -> URL {
(try FileManager.default.url(for: .itemReplacementDirectory, in: .userDomainMask, appropriateFor: URL(fileURLWithPath: "/"), create: true))
}
public func tempFile(suffix: String) throws -> URL {
let fileName = ProcessInfo.processInfo.globallyUniqueString + suffix
return (try tempDir()).appendingPathComponent(fileName)
}

View File

@ -77,11 +77,6 @@ struct PklGenSwift: AsyncParsableCommand {
) )
var pklInputModules: [String] = [] var pklInputModules: [String] = []
func tempFile() -> URL {
let fileName = ProcessInfo.processInfo.globallyUniqueString + ".pkl"
return URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(fileName)
}
private func generateScriptUrl() -> String { private func generateScriptUrl() -> String {
if let generateScript = self.generateScript { if let generateScript = self.generateScript {
return URL(fileURLWithPath: generateScript).path return URL(fileURLWithPath: generateScript).path

View File

@ -41,11 +41,6 @@ public struct PklSwiftGenerator {
} }
} }
private func tempFile() -> URL {
let fileName = ProcessInfo.processInfo.globallyUniqueString + ".pkl"
return URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(fileName)
}
private mutating func runModule(evaluator: Evaluator, pklInputModule: String) async throws { private mutating func runModule(evaluator: Evaluator, pklInputModule: String) async throws {
let out = resolvePaths(self.settings.outputPath ?? ".out") let out = resolvePaths(self.settings.outputPath ?? ".out")
let moduleToEvaluate = """ let moduleToEvaluate = """
@ -55,7 +50,7 @@ public struct PklSwiftGenerator {
moduleToGenerate = theModule moduleToGenerate = theModule
""" """
let tempFile = tempFile() let tempFile = try tempFile(suffix: ".pkl")
try moduleToEvaluate.write(to: tempFile, atomically: true, encoding: .utf8) try moduleToEvaluate.write(to: tempFile, atomically: true, encoding: .utf8)
let files = try await evaluator.evaluateOutputFiles(source: .url(tempFile)) let files = try await evaluator.evaluateOutputFiles(source: .url(tempFile))
for (filename, contents) in files { for (filename, contents) in files {

View File

@ -308,7 +308,7 @@ final class PklSwiftTests: XCTestCase {
// TODO re-enable this test when packages are available // TODO re-enable this test when packages are available
// //
// func testWithProject() async throws { // func testWithProject() async throws {
// let project1Dir = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("project1") // let project1Dir = (try tempDir()).appendingPathComponent("project1")
// try FileManager.default.createDirectory(at: project1Dir, withIntermediateDirectories: true) // try FileManager.default.createDirectory(at: project1Dir, withIntermediateDirectories: true)
// try """ // try """
// amends "pkl:Project" // amends "pkl:Project"

View File

@ -22,10 +22,10 @@ import XCTest
class ProjectTest: XCTestCase { class ProjectTest: XCTestCase {
func testLoadProject() async throws { func testLoadProject() async throws {
let version = try await SemanticVersion(EvaluatorManager().getVersion())! let version = try await SemanticVersion(EvaluatorManager().getVersion())!
let tempDir = NSTemporaryDirectory() let tempDir = try tempDir()
try FileManager.default.createDirectory(atPath: tempDir + "/subdir", withIntermediateDirectories: true) let subDir = tempDir.appendingPathComponent("subdir")
let otherProjectFile = URL(fileURLWithPath: tempDir, isDirectory: true) try FileManager.default.createDirectory(at: subDir, withIntermediateDirectories: true)
.appendingPathComponent("subdir/PklProject") let otherProjectFile = subDir.appendingPathComponent("PklProject")
try #""" try #"""
amends "pkl:Project" amends "pkl:Project"
@ -38,8 +38,8 @@ class ProjectTest: XCTestCase {
} }
"""#.write(to: otherProjectFile, atomically: true, encoding: .utf8) """#.write(to: otherProjectFile, atomically: true, encoding: .utf8)
let file = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true) let file = (try PklSwift.tempDir()).appendingPathComponent("PklProject")
.appendingPathComponent("PklProject")
let httpSetting = version < pklVersion0_26 ? "" : """ let httpSetting = version < pklVersion0_26 ? "" : """
http { http {
proxy { proxy {