Skip to content

Add 'PseudoTerminal' to 'TestSupport' #1720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Sources/TestSupport/PseudoTerminal.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
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
*/

@testable import Basic
import SPMLibc

public final class PseudoTerminal {
let master: Int32
let slave: Int32
public var outStream: LocalFileOutputByteStream

public init?(){
var master: Int32 = 0
var slave: Int32 = 0
if openpty(&master, &slave, nil, nil, nil) != 0 {
return nil
}
guard let outStream = try? LocalFileOutputByteStream(filePointer: fdopen(slave, "w"), closeOnDeinit: false) else {
return nil
}
self.outStream = outStream
self.master = master
self.slave = slave
}

public func readMaster(maxChars n: Int = 1000) -> String? {
var buf: [CChar] = [CChar](repeating: 0, count: n)
if read(master, &buf, n) <= 0 {
return nil
}
return String(cString: buf)
}

public func closeSlave() {
_ = SPMLibc.close(slave)
}

public func closeMaster() {
_ = SPMLibc.close(master)
}

public func close() {
closeSlave()
closeMaster()
}
}
36 changes: 2 additions & 34 deletions Tests/BasicTests/TerminalControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,9 @@
*/

import XCTest
@testable import Basic
import TestSupport
import SPMLibc

final class PseudoTerminal {
let master: Int32
let slave: Int32
var outStream: LocalFileOutputByteStream

init?(){
var master: Int32 = 0
var slave: Int32 = 0
if openpty(&master, &slave, nil, nil, nil) != 0 {
return nil
}
guard let outStream = try? LocalFileOutputByteStream(filePointer: fdopen(slave, "w"), closeOnDeinit: false) else {
return nil
}
self.outStream = outStream
self.master = master
self.slave = slave
}

func readMaster(maxChars n: Int = 1000) -> String? {
var buf: [CChar] = [CChar](repeating: 0, count: n)
if read(master, &buf, n) <= 0 {
return nil
}
return String(cString: buf)
}

func close() {
_ = SPMLibc.close(slave)
_ = SPMLibc.close(master)
}
}
@testable import Basic

final class TerminalControllerTests: XCTestCase {
func testBasic() {
Expand Down
38 changes: 1 addition & 37 deletions Tests/UtilityTests/ProgressBarTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,11 @@
import XCTest
import Utility
import SPMLibc
import TestSupport
@testable import Basic

typealias Thread = Basic.Thread

// FIXME: Copied from BasicTests, move to TestSupport once available.
final class PseudoTerminal {
let master: Int32
let slave: Int32
var outStream: LocalFileOutputByteStream

init?(){
var master: Int32 = 0
var slave: Int32 = 0
if openpty(&master, &slave, nil, nil, nil) != 0 {
return nil
}
guard let outStream = try? LocalFileOutputByteStream(filePointer: fdopen(slave, "w"), closeOnDeinit: false) else {
return nil
}
self.outStream = outStream
self.master = master
self.slave = slave
}

func readMaster(maxChars n: Int = 1000) -> String? {
var buf: [CChar] = [CChar](repeating: 0, count: n)
if read(master, &buf, n) <= 0 {
return nil
}
return String(cString: buf)
}

func closeSlave() {
_ = SPMLibc.close(slave)
}

func closeMaster() {
_ = SPMLibc.close(master)
}
}

final class ProgressBarTests: XCTestCase {
func testProgressBar() {
guard let pty = PseudoTerminal() else {
Expand Down