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