Tests/OrgAppTests/TangleWriterTests.swift
64 lines · 3414 bytes
1import Foundation
2import Testing
3@testable import OrgApp
4@testable import OrgCore
5
6struct TangleWriterTests {
7 @Test func writesMakesFoldersAndSetsModes() throws {
8 let folder = try Folder()
9 let source = try folder.write("notes.org", """
10 #+begin_src sh :tangle bin/run.sh :mkdirp yes :shebang "#!/bin/sh"
11 echo run
12 #+end_src
13 #+begin_src text :tangle missing/x.txt
14 x
15 #+end_src
16 """)
17 let text = try String(contentsOf: source, encoding: .utf8)
18 let output = try Tangle.run(text: text, path: source.path).get()
19 // The second file's folder is missing and it has no :mkdirp.
20 #expect(throws: TangleWriter.Failure.self) { try TangleWriter.write(output, source: source.path) }
21 let script = folder.url.appendingPathComponent("bin/run.sh")
22 #expect(try String(contentsOf: script, encoding: .utf8) == "#!/bin/sh\necho run\n")
23 let mode = try FileManager.default.attributesOfItem(atPath: script.path)[.posixPermissions] as? Int
24 #expect(mode == 0o755)
25 }
26
27 @Test func leavesUnchangedFilesAndRefusesItsOwnSource() throws {
28 let folder = try Folder()
29 let source = try folder.write("notes.org", "#+begin_src sh :tangle yes\necho a\n#+end_src\n")
30 let text = try String(contentsOf: source, encoding: .utf8)
31 let output = try Tangle.run(text: text, path: source.path).get()
32 let paths = try TangleWriter.write(output, source: source.path)
33 #expect(paths == [folder.url.appendingPathComponent("notes.sh").path])
34 let written = try FileManager.default.attributesOfItem(atPath: paths[0])[.modificationDate] as? Date
35 Thread.sleep(forTimeInterval: 1.1)
36 try TangleWriter.write(output, source: source.path)
37 #expect(try FileManager.default.attributesOfItem(atPath: paths[0])[.modificationDate] as? Date == written)
38 #expect(TangleWriter.message(output, source: source.path) == "Tangled 1 code block from notes.org")
39
40 let own = try Tangle.run(text: "#+begin_src org :tangle notes.org\nx\n#+end_src\n", path: source.path).get()
41 #expect(throws: TangleWriter.Failure(message: "Not allowed to tangle into the same file as self")) {
42 try TangleWriter.write(own, source: source.path)
43 }
44 }
45
46 @Test func aModeThatCantBeSetIsReportedAfterWriting() throws {
47 let folder = try Folder()
48 let script = folder.url.appendingPathComponent("run.sh").path
49 // Anyone can write /dev/null, but only its owner can change its mode.
50 let output = Tangle.Output(files: [
51 Tangle.File(path: "/dev/null", contents: "x\n", mode: 0o755, makeDirectories: false),
52 Tangle.File(path: script, contents: "#!/bin/sh\n", mode: 0o755, makeDirectories: false),
53 ], blocks: 2)
54 let source = folder.url.appendingPathComponent("notes.org").path
55 do {
56 try TangleWriter.write(output, source: source)
57 Issue.record("The mode of /dev/null was set")
58 } catch {
59 #expect(error.message.hasPrefix("Tangled 2 code blocks from notes.org, but can't set the mode of /dev/null"))
60 }
61 #expect(try String(contentsOfFile: script, encoding: .utf8) == "#!/bin/sh\n")
62 #expect(try FileManager.default.attributesOfItem(atPath: script)[.posixPermissions] as? Int == 0o755)
63 }
64}