Skip to content

Commit 58b3386

Browse files
committed
add coverage
1 parent 9590e22 commit 58b3386

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

inst/tests/tests.Rraw

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21865,7 +21865,10 @@ test(2345, fread('"this_that"\n"2025-01-01 00:00:01"'), data.table(this_that = a
2186521865

2186621866
# fread supports connections #561
2186721867
f = testDir("russellCRLF.csv")
21868-
test(2346.1, fread(file(f, "r")), fread(f))
21868+
test(2346.1, fread(file(f, "r"), verbose=TRUE), fread(f), output="Spill connection to tempfile")
2186921869
test(2346.2, fread(file(f, "r"), nrows=0L), fread(f, nrows=0L))
2187021870
test(2346.3, fread(file(f, "r"), nrows=5), fread(f, nrows=5))
2187121871
test(2346.4, fread(file(f, "r"), nrows=5, header=FALSE), fread(f, nrows=5, header=FALSE))
21872+
file.create(f <- tempfile())
21873+
test(2346.5, fread(file(f)), data.table(), warning="Connection has size 0.")
21874+
unlink(f)

src/freadR.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -743,24 +743,24 @@ void progress(int p, int eta)
743743
// Spill connection contents to a tempfile so R-level fread can treat it like a filename
744744
SEXP spillConnectionToFile(SEXP connection, SEXP tempfile_path, SEXP nrows_limit) {
745745
if (!inherits(connection, "connection")) {
746-
INTERNAL_STOP(_("spillConnectionToFile: argument must be a connection"));
746+
INTERNAL_STOP(_("spillConnectionToFile: argument must be a connection")); // # nocov
747747
}
748748

749749
if (!isString(tempfile_path) || LENGTH(tempfile_path) != 1) {
750-
INTERNAL_STOP(_("spillConnectionToFile: tempfile_path must be a single string"));
750+
INTERNAL_STOP(_("spillConnectionToFile: tempfile_path must be a single string")); // # nocov
751751
}
752752

753753
if (!isReal(nrows_limit) || LENGTH(nrows_limit) != 1) {
754-
INTERNAL_STOP(_("spillConnectionToFile: nrows_limit must be a single numeric value"));
754+
INTERNAL_STOP(_("spillConnectionToFile: nrows_limit must be a single numeric value")); // # nocov
755755
}
756756

757757
Rconnection con = R_GetConnection(connection);
758758
if (con == NULL) {
759-
INTERNAL_STOP(_("spillConnectionToFile: invalid connection"));
759+
INTERNAL_STOP(_("spillConnectionToFile: invalid connection")); // # nocov
760760
}
761761

762762
if (!con->isopen) {
763-
INTERNAL_STOP(_("spillConnectionToFile: connection is not open"));
763+
INTERNAL_STOP(_("spillConnectionToFile: connection is not open")); // # nocov
764764
}
765765

766766
const char *filepath = CHAR(STRING_ELT(tempfile_path, 0));
@@ -775,15 +775,15 @@ SEXP spillConnectionToFile(SEXP connection, SEXP tempfile_path, SEXP nrows_limit
775775

776776
FILE *outfile = fopen(filepath, "wb");
777777
if (outfile == NULL) {
778-
STOP(_("spillConnectionToFile: failed to open temp file '%s' for writing"), filepath);
778+
STOP(_("spillConnectionToFile: failed to open temp file '%s' for writing"), filepath); // # nocov
779779
}
780780

781781
// Read and write in chunks // TODO tune chunk size
782782
size_t chunk_size = 256 * 1024;
783783
char *buffer = malloc(chunk_size);
784784
if (!buffer) {
785-
fclose(outfile);
786-
STOP(_("spillConnectionToFile: failed to allocate buffer"));
785+
fclose(outfile); // # nocov
786+
STOP(_("spillConnectionToFile: failed to allocate buffer")); // # nocov
787787
}
788788

789789
size_t total_read = 0;
@@ -810,9 +810,11 @@ SEXP spillConnectionToFile(SEXP connection, SEXP tempfile_path, SEXP nrows_limit
810810

811811
size_t nwritten = fwrite(buffer, 1, bytes_to_write, outfile);
812812
if (nwritten != bytes_to_write) {
813+
// # nocov start
813814
free(buffer);
814815
fclose(outfile);
815816
STOP(_("spillConnectionToFile: write error (wrote %zu of %zu bytes)"), nwritten, bytes_to_write);
817+
// # nocov end
816818
}
817819
total_read += bytes_to_write;
818820

0 commit comments

Comments
 (0)