Skip to content

Commit 149f470

Browse files
JhonnyBillMaciidgh
authored andcommitted
Add 'PseudoTerminal' to 'TestSupport' (#1720)
* Add 'PseudoTerminal' to 'TestSupport' * Make 'PseudoTerminal' public * Separate back 'closeSlave()' and 'closeMaster()' * Removed a diff when calling 'closeSlave()' in 'ProgressBarTests'
1 parent 6d37f25 commit 149f470

File tree

3 files changed

+56
-71
lines changed

3 files changed

+56
-71
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
@testable import Basic
12+
import SPMLibc
13+
14+
public final class PseudoTerminal {
15+
let master: Int32
16+
let slave: Int32
17+
public var outStream: LocalFileOutputByteStream
18+
19+
public init?(){
20+
var master: Int32 = 0
21+
var slave: Int32 = 0
22+
if openpty(&master, &slave, nil, nil, nil) != 0 {
23+
return nil
24+
}
25+
guard let outStream = try? LocalFileOutputByteStream(filePointer: fdopen(slave, "w"), closeOnDeinit: false) else {
26+
return nil
27+
}
28+
self.outStream = outStream
29+
self.master = master
30+
self.slave = slave
31+
}
32+
33+
public func readMaster(maxChars n: Int = 1000) -> String? {
34+
var buf: [CChar] = [CChar](repeating: 0, count: n)
35+
if read(master, &buf, n) <= 0 {
36+
return nil
37+
}
38+
return String(cString: buf)
39+
}
40+
41+
public func closeSlave() {
42+
_ = SPMLibc.close(slave)
43+
}
44+
45+
public func closeMaster() {
46+
_ = SPMLibc.close(master)
47+
}
48+
49+
public func close() {
50+
closeSlave()
51+
closeMaster()
52+
}
53+
}

Tests/BasicTests/TerminalControllerTests.swift

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,9 @@
99
*/
1010

1111
import XCTest
12-
@testable import Basic
12+
import TestSupport
1313
import SPMLibc
14-
15-
final class PseudoTerminal {
16-
let master: Int32
17-
let slave: Int32
18-
var outStream: LocalFileOutputByteStream
19-
20-
init?(){
21-
var master: Int32 = 0
22-
var slave: Int32 = 0
23-
if openpty(&master, &slave, nil, nil, nil) != 0 {
24-
return nil
25-
}
26-
guard let outStream = try? LocalFileOutputByteStream(filePointer: fdopen(slave, "w"), closeOnDeinit: false) else {
27-
return nil
28-
}
29-
self.outStream = outStream
30-
self.master = master
31-
self.slave = slave
32-
}
33-
34-
func readMaster(maxChars n: Int = 1000) -> String? {
35-
var buf: [CChar] = [CChar](repeating: 0, count: n)
36-
if read(master, &buf, n) <= 0 {
37-
return nil
38-
}
39-
return String(cString: buf)
40-
}
41-
42-
func close() {
43-
_ = SPMLibc.close(slave)
44-
_ = SPMLibc.close(master)
45-
}
46-
}
14+
@testable import Basic
4715

4816
final class TerminalControllerTests: XCTestCase {
4917
func testBasic() {

Tests/UtilityTests/ProgressBarTests.swift

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,47 +11,11 @@
1111
import XCTest
1212
import Utility
1313
import SPMLibc
14+
import TestSupport
1415
@testable import Basic
1516

1617
typealias Thread = Basic.Thread
1718

18-
// FIXME: Copied from BasicTests, move to TestSupport once available.
19-
final class PseudoTerminal {
20-
let master: Int32
21-
let slave: Int32
22-
var outStream: LocalFileOutputByteStream
23-
24-
init?(){
25-
var master: Int32 = 0
26-
var slave: Int32 = 0
27-
if openpty(&master, &slave, nil, nil, nil) != 0 {
28-
return nil
29-
}
30-
guard let outStream = try? LocalFileOutputByteStream(filePointer: fdopen(slave, "w"), closeOnDeinit: false) else {
31-
return nil
32-
}
33-
self.outStream = outStream
34-
self.master = master
35-
self.slave = slave
36-
}
37-
38-
func readMaster(maxChars n: Int = 1000) -> String? {
39-
var buf: [CChar] = [CChar](repeating: 0, count: n)
40-
if read(master, &buf, n) <= 0 {
41-
return nil
42-
}
43-
return String(cString: buf)
44-
}
45-
46-
func closeSlave() {
47-
_ = SPMLibc.close(slave)
48-
}
49-
50-
func closeMaster() {
51-
_ = SPMLibc.close(master)
52-
}
53-
}
54-
5519
final class ProgressBarTests: XCTestCase {
5620
func testProgressBar() {
5721
guard let pty = PseudoTerminal() else {

0 commit comments

Comments
 (0)