|
| 1 | +import XCTest |
| 2 | +@testable import SwiftGit |
| 3 | + |
| 4 | +final class PlumbingTests: XCTestCase { |
| 5 | + |
| 6 | + func makeTempDir(_ name: String = "swiftgit-plumb") throws -> URL { |
| 7 | + let fm = FileManager.default |
| 8 | + let base = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(name + "-\(UUID().uuidString)") |
| 9 | + try fm.createDirectory(at: base, withIntermediateDirectories: true) |
| 10 | + return base |
| 11 | + } |
| 12 | + |
| 13 | + func envOrSkip() throws -> GitEnvironment { |
| 14 | + if let env = try? GitEnvironment(type: .embed) { return env } |
| 15 | + if let env = try? GitEnvironment(type: .system) { return env } |
| 16 | + throw XCTSkip("No git instance available (embed or system)") |
| 17 | + } |
| 18 | + |
| 19 | + func test_update_index_add_and_write_tree() throws { |
| 20 | + let env = try envOrSkip() |
| 21 | + let git = try Git(environment: env) |
| 22 | + let dir = try makeTempDir() |
| 23 | + try git.run(["init"], context: Shell.Context(at: dir)) |
| 24 | + try git.run(["config", "user.name", "P"], context: Shell.Context(at: dir)) |
| 25 | + try git.run(["config", "user.email", "p@e"], context: Shell.Context(at: dir)) |
| 26 | + |
| 27 | + // create files |
| 28 | + try "one".write(to: dir.appendingPathComponent("one.txt"), atomically: true, encoding: .utf8) |
| 29 | + try "two".write(to: dir.appendingPathComponent("two.txt"), atomically: true, encoding: .utf8) |
| 30 | + |
| 31 | + // use update-index to add paths to index |
| 32 | + try git.run(["update-index", "--add", "one.txt"], context: Shell.Context(at: dir)) |
| 33 | + try git.run(["update-index", "--add", "two.txt"], context: Shell.Context(at: dir)) |
| 34 | + |
| 35 | + // write-tree should produce a tree object |
| 36 | + let tree = try git.run(["write-tree"], context: Shell.Context(at: dir)) |
| 37 | + let t = tree.trimmingCharacters(in: .whitespacesAndNewlines) |
| 38 | + XCTAssertFalse(t.isEmpty) |
| 39 | + let typ = try git.run(["cat-file", "-t", t], context: Shell.Context(at: dir)) |
| 40 | + XCTAssertEqual(typ.trimmingCharacters(in: .whitespacesAndNewlines), "tree") |
| 41 | + } |
| 42 | + |
| 43 | + func test_ls_files_ls_tree_rev_list_and_show_ref() throws { |
| 44 | + let env = try envOrSkip() |
| 45 | + let git = try Git(environment: env) |
| 46 | + let dir = try makeTempDir() |
| 47 | + try git.run(["init"], context: Shell.Context(at: dir)) |
| 48 | + try git.run(["config", "user.name", "Q"], context: Shell.Context(at: dir)) |
| 49 | + try git.run(["config", "user.email", "q@e"], context: Shell.Context(at: dir)) |
| 50 | + |
| 51 | + try "alpha".write(to: dir.appendingPathComponent("a.txt"), atomically: true, encoding: .utf8) |
| 52 | + try git.run(["add", "a.txt"], context: Shell.Context(at: dir)) |
| 53 | + try git.run(["commit", "-m", "c1"], context: Shell.Context(at: dir)) |
| 54 | + |
| 55 | + // ls-files should include a.txt |
| 56 | + let ls = try git.run(["ls-files"], context: Shell.Context(at: dir)) |
| 57 | + XCTAssertTrue(ls.contains("a.txt")) |
| 58 | + |
| 59 | + // rev-list count should be >= 1 |
| 60 | + let count = try git.run(["rev-list", "--count", "HEAD"], context: Shell.Context(at: dir)) |
| 61 | + XCTAssertTrue((Int(count.trimmingCharacters(in: .whitespacesAndNewlines)) ?? 0) >= 1) |
| 62 | + |
| 63 | + // ls-tree should show the file in HEAD |
| 64 | + let lst = try git.run(["ls-tree", "HEAD"], context: Shell.Context(at: dir)) |
| 65 | + XCTAssertTrue(lst.contains("a.txt")) |
| 66 | + |
| 67 | + // show-ref should include HEAD's branch ref |
| 68 | + let branch = try git.run(["rev-parse", "--abbrev-ref", "HEAD"], context: Shell.Context(at: dir)) |
| 69 | + let refs = try git.run(["show-ref"], context: Shell.Context(at: dir)) |
| 70 | + XCTAssertTrue(refs.contains(branch.trimmingCharacters(in: .whitespacesAndNewlines))) |
| 71 | + } |
| 72 | + |
| 73 | + func test_update_ref_creates_new_ref() throws { |
| 74 | + let env = try envOrSkip() |
| 75 | + let git = try Git(environment: env) |
| 76 | + let dir = try makeTempDir() |
| 77 | + try git.run(["init"], context: Shell.Context(at: dir)) |
| 78 | + try git.run(["config", "user.name", "U"], context: Shell.Context(at: dir)) |
| 79 | + try git.run(["config", "user.email", "u@e"], context: Shell.Context(at: dir)) |
| 80 | + |
| 81 | + try "x".write(to: dir.appendingPathComponent("x.txt"), atomically: true, encoding: .utf8) |
| 82 | + try git.run(["add", "x.txt"], context: Shell.Context(at: dir)) |
| 83 | + try git.run(["commit", "-m", "c"], context: Shell.Context(at: dir)) |
| 84 | + let head = try git.run(["rev-parse", "HEAD"], context: Shell.Context(at: dir)) |
| 85 | + let commitHash = head.trimmingCharacters(in: .whitespacesAndNewlines) |
| 86 | + |
| 87 | + try git.run(["update-ref", "refs/heads/testref", commitHash], context: Shell.Context(at: dir)) |
| 88 | + let refs = try git.run(["for-each-ref", "--format=%(refname)"], context: Shell.Context(at: dir)) |
| 89 | + XCTAssertTrue(refs.contains("refs/heads/testref")) |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments