lmi
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi] What does this test do?


From: Greg Chicares
Subject: [lmi] What does this test do?
Date: Mon, 3 May 2021 14:50:05 +0000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.9.0

Vadim--in a file that hasn't changed substantially since its 2005 original,
I see:

    fs::directory_iterator const i(path);
    LMI_TEST(i->exists());

and wonder: what was I thinking? What does it mean when that test succeeds?
If I were to write a comment explaining it, what would I say?

One might imagine that what seems weird when isolated, might make sense in
context; but 'i' has never been used elsewhere, neither in the 2005 original
nor in the other (unremarkable) commit found thus:
  $ git log --patch -G'directory_iterator' -- global_settings_test.cpp

I think the quoted code means that 'path' names a directory, and that
 - the directory exists, and
 - it contains at least one entry other than '.' and '..', and
 - the first pointed-to entry refers to an existing filesystem object.
That seems like a strange way of testing that the directory
is not empty. And, if it is empty, wouldn't dereferencing 'i' be UB?

I'd like to replace those lines with something I can understand, e.g.:
  LMI_TEST(is_directory(path));
Poring over the Standard leads me to believe is_directory() is
implemented in terms of status(), which behaves as if implemented
in terms of posix stat(). And we can't 'stat' a file that doesn't
exist, AFAIK, so if 'true == is_directory(path)', then the directory
necessarily exists. Therefore, it's not necessary to add an explicit
existence test:

    LMI_TEST(is_directory(path)); // Success implies existence.
//  LMI_TEST(path.exists());      // Unnecessary.

and if we really do want to be sure the directory is not empty:

    LMI_TEST(is_directory(path)); //
    LMI_TEST(!is_empty(path));    // Of dubious value.

but I don't think nonemptiness is an invariant that we should
try to enforce here: if running this unit test is the first
thing we do in a brand-new installation, we might not have
populated this directory yet.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]