Skip to content

Commit 9bc3704

Browse files
authored
Fix tests to avoid depending on the contents of Cargo.toml. (#1401)
Cargo.toml gets modified when the package is published, so don't use it as input data for tests. Instead, have the tests use their own source file as input. Fixes #1398.
1 parent bcac8fd commit 9bc3704

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/buffer.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,8 @@ mod tests {
321321
use crate::io::read;
322322
use core::mem::MaybeUninit;
323323

324-
let input = std::fs::File::open("Cargo.toml").unwrap();
324+
// We need to obtain input stream, so open our own source file.
325+
let input = std::fs::File::open("src/buffer.rs").unwrap();
325326

326327
let mut buf = vec![0_u8; 3];
327328
buf.reserve(32);
@@ -360,12 +361,14 @@ mod tests {
360361
use crate::io::read;
361362
use std::io::{Seek, SeekFrom};
362363

363-
let mut input = std::fs::File::open("Cargo.toml").unwrap();
364+
// We need to obtain input stream with contents that we can compare
365+
// against, so open our own source file.
366+
let mut input = std::fs::File::open("src/buffer.rs").unwrap();
364367

365368
let mut buf = [0_u8; 64];
366369
let nread = read(&input, &mut buf).unwrap();
367370
assert_eq!(nread, buf.len());
368-
assert_eq!(&buf[..9], b"[package]");
371+
assert_eq!(&buf[..38], b"//! Utilities to help with buffering.\n");
369372
input.seek(SeekFrom::End(-1)).unwrap();
370373
let nread = read(&input, &mut buf).unwrap();
371374
assert_eq!(nread, 1);
@@ -381,16 +384,18 @@ mod tests {
381384
use core::mem::MaybeUninit;
382385
use std::io::{Seek, SeekFrom};
383386

384-
let mut input = std::fs::File::open("Cargo.toml").unwrap();
387+
// We need to obtain input stream with contents that we can compare
388+
// against, so open our own source file.
389+
let mut input = std::fs::File::open("src/buffer.rs").unwrap();
385390

386391
let mut buf = [MaybeUninit::<u8>::uninit(); 64];
387392
let (init, uninit) = read(&input, &mut buf).unwrap();
388393
assert_eq!(uninit.len(), 0);
389-
assert_eq!(&init[..9], b"[package]");
394+
assert_eq!(&init[..38], b"//! Utilities to help with buffering.\n");
390395
assert_eq!(init.len(), buf.len());
391396
assert_eq!(
392-
unsafe { core::mem::transmute::<&mut [MaybeUninit<u8>], &mut [u8]>(&mut buf[..9]) },
393-
b"[package]"
397+
unsafe { core::mem::transmute::<&mut [MaybeUninit<u8>], &mut [u8]>(&mut buf[..38]) },
398+
b"//! Utilities to help with buffering.\n"
394399
);
395400
input.seek(SeekFrom::End(-1)).unwrap();
396401
let (init, uninit) = read(&input, &mut buf).unwrap();
@@ -408,13 +413,15 @@ mod tests {
408413
use crate::io::read;
409414
use std::io::{Seek, SeekFrom};
410415

411-
let mut input = std::fs::File::open("Cargo.toml").unwrap();
416+
// We need to obtain input stream with contents that we can compare
417+
// against, so open our own source file.
418+
let mut input = std::fs::File::open("src/buffer.rs").unwrap();
412419

413420
let mut buf = Vec::with_capacity(64);
414421
let nread = read(&input, spare_capacity(&mut buf)).unwrap();
415422
assert_eq!(nread, buf.capacity());
416423
assert_eq!(nread, buf.len());
417-
assert_eq!(&buf[..9], b"[package]");
424+
assert_eq!(&buf[..38], b"//! Utilities to help with buffering.\n");
418425
buf.clear();
419426
input.seek(SeekFrom::End(-1)).unwrap();
420427
let nread = read(&input, spare_capacity(&mut buf)).unwrap();

0 commit comments

Comments
 (0)