1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import XCTest
import TSCUtility
import TSCLibc
import TSCTestSupport
import TSCBasic
typealias Thread = TSCBasic.Thread
final class ProgressAnimationTests: XCTestCase {
func testPercentProgressAnimationDumbTerminal() {
var outStream = BufferedOutputByteStream()
var animation = PercentProgressAnimation(stream: outStream, header: "TestHeader")
runProgressAnimation(animation)
XCTAssertEqual(outStream.bytes.validDescription, """
TestHeader
0%: 0
10%: 1
20%: 2
30%: 3
40%: 4
50%: 5
""")
outStream = BufferedOutputByteStream()
animation = PercentProgressAnimation(stream: outStream, header: "TestHeader")
animation.complete(success: true)
XCTAssertEqual(outStream.bytes.validDescription, "")
}
func testNinjaProgressAnimationDumbTerminal() {
var outStream = BufferedOutputByteStream()
var animation = NinjaProgressAnimation(stream: outStream)
runProgressAnimation(animation)
XCTAssertEqual(outStream.bytes.validDescription, """
[0/10] 0
[1/10] 1
[2/10] 2
[3/10] 3
[4/10] 4
[5/10] 5
""")
outStream = BufferedOutputByteStream()
animation = NinjaProgressAnimation(stream: outStream)
animation.complete(success: true)
XCTAssertEqual(outStream.bytes.validDescription, "")
}
#if !os(Windows) // PseudoTerminal is not supported in Windows
func testPercentProgressAnimationTTY() throws {
let output = try readingTTY { tty in
let animation = PercentProgressAnimation(stream: tty.outStream, header: "TestHeader")
runProgressAnimation(animation)
}
let startCyan = "\u{1B}[36m"
let bold = "\u{1B}[1m"
let end = "\u{1B}[0m"
XCTAssertMatch(output.spm_chuzzle(), .prefix("\(startCyan)\(bold)TestHeader\(end)"))
}
func testNinjaProgressAnimationTTY() throws {
var output = try readingTTY { tty in
let animation = NinjaProgressAnimation(stream: tty.outStream)
runProgressAnimation(animation)
}
let clearLine = "\u{1B}[2K\r"
let newline = "\r\n"
XCTAssertEqual(output, """
\(clearLine)[0/10] 0\
\(clearLine)[1/10] 1\
\(clearLine)[2/10] 2\
\(clearLine)[3/10] 3\
\(clearLine)[4/10] 4\
\(clearLine)[5/10] 5\(newline)
""")
output = try readingTTY { tty in
let animation = NinjaProgressAnimation(stream: tty.outStream)
animation.complete(success: true)
}
XCTAssertEqual(output, "")
}
private func readingTTY(_ closure: (PseudoTerminal) -> Void) throws -> String {
guard let terminal = PseudoTerminal() else {
struct PseudoTerminalCreationError: Error {}
throw PseudoTerminalCreationError()
}
var output = ""
let thread = Thread {
while let out = terminal.readPrimary() {
output += out
}
}
thread.start()
closure(terminal)
terminal.closeSecondary()
// Make sure to read the complete output before checking it.
thread.join()
terminal.closePrimary()
return output
}
#endif
private func runProgressAnimation(_ animation: ProgressAnimationProtocol) {
for i in 0...5 {
animation.update(step: i, total: 10, text: String(i))
}
animation.complete(success: true)
}
}
|