Skip to content

Commit

Permalink
Another round of test fixes and merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed Oct 24, 2013
1 parent d425218 commit 188e471
Show file tree
Hide file tree
Showing 16 changed files with 264 additions and 174 deletions.
4 changes: 2 additions & 2 deletions src/libextra/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ fn decode_inner(s: &str, full_url: bool) -> ~str {
let mut bytes = [0, 0];
match rdr.read(bytes) {
Some(2) => {}
_ => fail2!() // XXX: malformed url?
_ => fail!() // XXX: malformed url?
}
let ch = uint::parse_bytes(bytes, 16u).unwrap() as u8 as char;

Expand Down Expand Up @@ -279,7 +279,7 @@ pub fn decode_form_urlencoded(s: &[u8]) -> HashMap<~str, ~[~str]> {
let mut bytes = [0, 0];
match rdr.read(bytes) {
Some(2) => {}
_ => fail2!() // XXX: malformed?
_ => fail!() // XXX: malformed?
}
uint::parse_bytes(bytes, 16u).unwrap() as u8 as char
}
Expand Down
19 changes: 12 additions & 7 deletions src/librustpkg/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
// rustpkg unit tests

use context::{BuildContext, Context, RustcFlags};
use std::{io, os, run, str, task};
use std::{os, run, str, task};
use std::rt::io;
use std::rt::io::Writer;
use std::rt::io::file::FileInfo;
use extra::arc::Arc;
use extra::arc::RWArc;
use extra::tempfile::TempDir;
Expand Down Expand Up @@ -81,8 +84,9 @@ fn git_repo_pkg_with_tag(a_tag: ~str) -> PkgId {
}

fn writeFile(file_path: &Path, contents: &str) {
let out = io::file_writer(file_path, [io::Create, io::Truncate]).unwrap();
out.write_line(contents);
let mut out = file_path.open_writer(io::CreateOrTruncate);
out.write(contents.as_bytes());
out.write(['\n' as u8]);
}

fn mk_emptier_workspace(tag: &str) -> TempDir {
Expand Down Expand Up @@ -550,10 +554,11 @@ fn frob_source_file(workspace: &Path, pkgid: &PkgId, filename: &str) {
debug!("Frobbed? {:?}", maybe_p);
match maybe_p {
Some(ref p) => {
let w = io::file_writer(p, &[io::Append]);
match w {
Err(s) => { let _ = cond.raise((p.clone(), format!("Bad path: {}", s))); }
Ok(w) => w.write_line("/* hi */")
do io::io_error::cond.trap(|e| {
cond.raise((p.clone(), format!("Bad path: {}", e.desc)));
}).inside {
let mut w = p.open_writer(io::Append);
w.write(bytes!("/* hi */\n"));
}
}
None => fail!("frob_source_file failed to find a source file in {}",
Expand Down
4 changes: 2 additions & 2 deletions src/librustpkg/testsuite/pass/src/c-dependencies/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
extern mod rustpkg;
extern mod rustc;

use std::{io, os, task};
use std::{os, task};
use rustpkg::api;
use rustpkg::version::NoVersion;
use rustpkg::workcache_support::digest_file_with_date;
Expand All @@ -36,7 +36,7 @@ pub fn main() {
}

if args[2] != ~"install" {
io::println(format!("Warning: I don't know how to {}", args[2]));
println(format!("Warning: I don't know how to {}", args[2]));
return;
}

Expand Down
11 changes: 7 additions & 4 deletions src/librustpkg/testsuite/pass/src/fancy-lib/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
extern mod rustpkg;
extern mod rustc;

use std::{io, os};
use std::os;
use std::rt::io;
use std::rt::io::Writer;
use std::rt::io::file::FileInfo;
use rustpkg::api;
use rustpkg::version::NoVersion;

Expand Down Expand Up @@ -42,9 +45,9 @@ pub fn main() {
let out_path = os::self_exe_path().expect("Couldn't get self_exe path");

debug!("Writing file");
let file = io::file_writer(&out_path.join("generated.rs"), [io::Create]).unwrap();
file.write_str("pub fn wheeeee() { let xs = [1, 2, 3]; \
for _ in xs.iter() { assert!(true); } }");
let mut file = out_path.join("generated.rs").open_writer(io::Create);
file.write("pub fn wheeeee() { let xs = [1, 2, 3]; \
for _ in xs.iter() { assert!(true); } }".as_bytes());

let context = api::default_context(sysroot, api::default_workspace());
api::install_pkg(&context, os::getcwd(), ~"fancy-lib", NoVersion, ~[]);
Expand Down
25 changes: 16 additions & 9 deletions src/libstd/rt/io/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ impl Listener {
mod test {
use libc;
use rt::io::timer;
use rt::io;
use super::*;

// kill is only available on Unixes
Expand All @@ -158,19 +159,19 @@ mod test {
}
}

#[test]
#[test] #[cfg(unix)]
fn test_io_signal_smoketest() {
let mut signal = Listener::new();
signal.register(Interrupt);
sigint();
timer::sleep(10);
match signal.port.recv() {
Interrupt => (),
s => fail2!("Expected Interrupt, got {:?}", s),
s => fail!("Expected Interrupt, got {:?}", s),
}
}

#[test]
#[test] #[cfg(unix)]
fn test_io_signal_two_signal_one_signum() {
let mut s1 = Listener::new();
let mut s2 = Listener::new();
Expand All @@ -180,15 +181,15 @@ mod test {
timer::sleep(10);
match s1.port.recv() {
Interrupt => (),
s => fail2!("Expected Interrupt, got {:?}", s),
s => fail!("Expected Interrupt, got {:?}", s),
}
match s1.port.recv() {
Interrupt => (),
s => fail2!("Expected Interrupt, got {:?}", s),
s => fail!("Expected Interrupt, got {:?}", s),
}
}

#[test]
#[test] #[cfg(unix)]
fn test_io_signal_unregister() {
let mut s1 = Listener::new();
let mut s2 = Listener::new();
Expand All @@ -198,16 +199,22 @@ mod test {
sigint();
timer::sleep(10);
if s2.port.peek() {
fail2!("Unexpected {:?}", s2.port.recv());
fail!("Unexpected {:?}", s2.port.recv());
}
}

#[cfg(windows)]
#[test]
fn test_io_signal_invalid_signum() {
let mut s = Listener::new();
if s.register(User1) {
fail2!("Unexpected successful registry of signum {:?}", User1);
let mut called = false;
do io::io_error::cond.trap(|_| {
called = true;
}).inside {
if s.register(User1) {
fail!("Unexpected successful registry of signum {:?}", User1);
}
}
assert!(called);
}
}
133 changes: 99 additions & 34 deletions src/libstd/rt/io/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,57 @@ use fmt;
use libc;
use option::{Option, Some, None};
use result::{Ok, Err};
use rt::rtio::{IoFactory, RtioTTY, with_local_io, RtioPipe};
use super::{Reader, Writer, io_error};
use rt::rtio::{IoFactory, RtioTTY, RtioFileStream, with_local_io,
CloseAsynchronously};
use super::{Reader, Writer, io_error, IoError, OtherIoError};

// And so begins the tale of acquiring a uv handle to a stdio stream on all
// platforms in all situations. Our story begins by splitting the world into two
// categories, windows and unix. Then one day the creators of unix said let
// there be redirection! And henceforth there was redirection away from the
// console for standard I/O streams.
//
// After this day, the world split into four factions:
//
// 1. Unix with stdout on a terminal.
// 2. Unix with stdout redirected.
// 3. Windows with stdout on a terminal.
// 4. Windows with stdout redirected.
//
// Many years passed, and then one day the nation of libuv decided to unify this
// world. After months of toiling, uv created three ideas: TTY, Pipe, File.
// These three ideas propagated throughout the lands and the four great factions
// decided to settle among them.
//
// The groups of 1, 2, and 3 all worked very hard towards the idea of TTY. Upon
// doing so, they even enhanced themselves further then their Pipe/File
// brethren, becoming the dominant powers.
//
// The group of 4, however, decided to work independently. They abandoned the
// common TTY belief throughout, and even abandoned the fledgling Pipe belief.
// The members of the 4th faction decided to only align themselves with File.
//
// tl;dr; TTY works on everything but when windows stdout is redirected, in that
// case pipe also doesn't work, but magically file does!
enum StdSource {
TTY(~RtioTTY),
File(~RtioFileStream),
}

#[fixed_stack_segment] #[inline(never)]
fn tty<T>(fd: libc::c_int, f: &fn(~RtioTTY) -> T) -> T {
fn src<T>(fd: libc::c_int, readable: bool, f: &fn(StdSource) -> T) -> T {
do with_local_io |io| {
// Always pass in readable as true, otherwise libuv turns our writes
// into blocking writes. We also need to dup the file descriptor because
// the tty will be closed when it's dropped.
match io.tty_open(unsafe { libc::dup(fd) }, true) {
Ok(tty) => Some(f(tty)),
Err(e) => {
io_error::cond.raise(e);
None
let fd = unsafe { libc::dup(fd) };
match io.tty_open(fd, readable) {
Ok(tty) => Some(f(TTY(tty))),
Err(_) => {
// It's not really that desirable if these handles are closed
// synchronously, and because they're squirreled away in a task
// structure the destructors will be run when the task is
// attempted to get destroyed. This means that if we run a
// synchronous destructor we'll attempt to do some scheduling
// operations which will just result in sadness.
Some(f(File(io.fs_from_raw_fd(fd, CloseAsynchronously))))
}
}
}.unwrap()
Expand All @@ -54,15 +91,7 @@ fn tty<T>(fd: libc::c_int, f: &fn(~RtioTTY) -> T) -> T {
/// See `stdout()` for notes about this function.
#[fixed_stack_segment] #[inline(never)]
pub fn stdin() -> StdReader {
do with_local_io |io| {
match io.pipe_open(unsafe { libc::dup(libc::STDIN_FILENO) }) {
Ok(stream) => Some(StdReader { inner: stream }),
Err(e) => {
io_error::cond.raise(e);
None
}
}
}.unwrap()
do src(libc::STDIN_FILENO, true) |src| { StdReader { inner: src } }
}

/// Creates a new non-blocking handle to the stdout of the current process.
Expand All @@ -72,14 +101,14 @@ pub fn stdin() -> StdReader {
/// task context because the stream returned will be a non-blocking object using
/// the local scheduler to perform the I/O.
pub fn stdout() -> StdWriter {
do tty(libc::STDOUT_FILENO) |tty| { StdWriter { inner: tty } }
do src(libc::STDOUT_FILENO, false) |src| { StdWriter { inner: src } }
}

/// Creates a new non-blocking handle to the stderr of the current process.
///
/// See `stdout()` for notes about this function.
pub fn stderr() -> StdWriter {
do tty(libc::STDERR_FILENO) |tty| { StdWriter { inner: tty } }
do src(libc::STDERR_FILENO, false) |src| { StdWriter { inner: src } }
}

/// Prints a string to the stdout of the current process. No newline is emitted
Expand Down Expand Up @@ -117,12 +146,16 @@ pub fn println_args(fmt: &fmt::Arguments) {

/// Representation of a reader of a standard input stream
pub struct StdReader {
priv inner: ~RtioPipe
priv inner: StdSource
}

impl Reader for StdReader {
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
match self.inner.read(buf) {
let ret = match self.inner {
TTY(ref mut tty) => tty.read(buf),
File(ref mut file) => file.read(buf).map_move(|i| i as uint),
};
match ret {
Ok(amt) => Some(amt as uint),
Err(e) => {
io_error::cond.raise(e);
Expand All @@ -136,7 +169,7 @@ impl Reader for StdReader {

/// Representation of a writer to a standard output stream
pub struct StdWriter {
priv inner: ~RtioTTY
priv inner: StdSource
}

impl StdWriter {
Expand All @@ -151,10 +184,22 @@ impl StdWriter {
/// This function will raise on the `io_error` condition if an error
/// happens.
pub fn winsize(&mut self) -> Option<(int, int)> {
match self.inner.get_winsize() {
Ok(p) => Some(p),
Err(e) => {
io_error::cond.raise(e);
match self.inner {
TTY(ref mut tty) => {
match tty.get_winsize() {
Ok(p) => Some(p),
Err(e) => {
io_error::cond.raise(e);
None
}
}
}
File(*) => {
io_error::cond.raise(IoError {
kind: OtherIoError,
desc: "stream is not a tty",
detail: None,
});
None
}
}
Expand All @@ -168,21 +213,41 @@ impl StdWriter {
/// This function will raise on the `io_error` condition if an error
/// happens.
pub fn set_raw(&mut self, raw: bool) {
match self.inner.set_raw(raw) {
Ok(()) => {},
Err(e) => io_error::cond.raise(e),
match self.inner {
TTY(ref mut tty) => {
match tty.set_raw(raw) {
Ok(()) => {},
Err(e) => io_error::cond.raise(e),
}
}
File(*) => {
io_error::cond.raise(IoError {
kind: OtherIoError,
desc: "stream is not a tty",
detail: None,
});
}
}
}

/// Returns whether this tream is attached to a TTY instance or not.
///
/// This is similar to libc's isatty() function
pub fn isatty(&self) -> bool { self.inner.isatty() }
pub fn isatty(&self) -> bool {
match self.inner {
TTY(ref tty) => tty.isatty(),
File(*) => false,
}
}
}

impl Writer for StdWriter {
fn write(&mut self, buf: &[u8]) {
match self.inner.write(buf) {
let ret = match self.inner {
TTY(ref mut tty) => tty.write(buf),
File(ref mut file) => file.write(buf),
};
match ret {
Ok(()) => {}
Err(e) => io_error::cond.raise(e)
}
Expand Down
Loading

5 comments on commit 188e471

@bors
Copy link
Contributor

@bors bors commented on 188e471 Oct 24, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from brson
at alexcrichton@188e471

@bors
Copy link
Contributor

@bors bors commented on 188e471 Oct 24, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging alexcrichton/rust/unix-sockets = 188e471 into auto

@bors
Copy link
Contributor

@bors bors commented on 188e471 Oct 24, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alexcrichton/rust/unix-sockets = 188e471 merged ok, testing candidate = 3f5b221

@bors
Copy link
Contributor

@bors bors commented on 188e471 Oct 24, 2013

@bors
Copy link
Contributor

@bors bors commented on 188e471 Oct 24, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 3f5b221

Please sign in to comment.