Skip to content

Commit 2ab8be7

Browse files
authored
Cleanup test_readdir_r_silly. NFC (#22804)
1 parent fb2ae61 commit 2ab8be7

File tree

1 file changed

+32
-37
lines changed

1 file changed

+32
-37
lines changed

test/test_other.py

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5636,6 +5636,7 @@ def test_rename_silly(self):
56365636

56375637
def test_readdir_r_silly(self):
56385638
create_file('src.cpp', r'''
5639+
#include <cassert>
56395640
#include <iostream>
56405641
#include <cstring>
56415642
#include <cerrno>
@@ -5645,29 +5646,23 @@ def test_readdir_r_silly(self):
56455646
#include <dirent.h>
56465647
#include <sys/stat.h>
56475648
#include <sys/types.h>
5649+
56485650
using std::endl;
5651+
56495652
namespace
56505653
{
5651-
void check(const bool result)
5652-
{
5653-
if(not result) {
5654-
std::cout << "Check failed!" << endl;
5655-
throw "bad";
5656-
}
5657-
}
56585654
// Do a recursive directory listing of the directory whose path is specified
56595655
// by \a name.
5660-
void ls(const std::string& name, std::size_t indent = 0)
5661-
{
5662-
::DIR *dir;
5663-
struct ::dirent *entry;
5664-
if(indent == 0) {
5656+
void ls(const std::string& name, size_t indent = 0) {
5657+
DIR *dir;
5658+
struct dirent *entry;
5659+
if (indent == 0) {
56655660
std::cout << name << endl;
56665661
++indent;
56675662
}
56685663
// Make sure we can open the directory. This should also catch cases where
56695664
// the empty string is passed in.
5670-
if (not (dir = ::opendir(name.c_str()))) {
5665+
if (not (dir = opendir(name.c_str()))) {
56715666
const int error = errno;
56725667
std::cout
56735668
<< "Failed to open directory: " << name << "; " << error << endl;
@@ -5678,17 +5673,17 @@ def test_readdir_r_silly(self):
56785673
std::cout
56795674
<< "Managed to open a directory whose name was the empty string.."
56805675
<< endl;
5681-
check(::closedir(dir) != -1);
5676+
assert(closedir(dir) != -1);
56825677
return;
56835678
}
56845679
// Iterate over the entries in the directory.
5685-
while ((entry = ::readdir(dir))) {
5686-
const std::string entryName(entry->d_name);
5680+
while ((entry = readdir(dir))) {
5681+
std::string entryName(entry->d_name);
56875682
if (entryName == "." || entryName == "..") {
56885683
// Skip the dot entries.
56895684
continue;
56905685
}
5691-
const std::string indentStr(indent * 2, ' ');
5686+
std::string indentStr(indent * 2, ' ');
56925687
if (entryName.empty()) {
56935688
std::cout
56945689
<< indentStr << "\"\": Found empty string as a "
@@ -5706,18 +5701,18 @@ def test_readdir_r_silly(self):
57065701
}
57075702
}
57085703
// Close our handle.
5709-
check(::closedir(dir) != -1);
5704+
assert(closedir(dir) != -1);
57105705
}
5711-
void touch(const std::string &path)
5712-
{
5713-
const int fd = ::open(path.c_str(), O_CREAT | O_TRUNC, 0644);
5714-
check(fd != -1);
5715-
check(::close(fd) != -1);
5706+
5707+
void touch(const char* path) {
5708+
int fd = open(path, O_CREAT | O_TRUNC, 0644);
5709+
assert(fd != -1);
5710+
assert(close(fd) != -1);
57165711
}
57175712
}
5718-
int main()
5719-
{
5720-
check(::mkdir("dir", 0755) == 0);
5713+
5714+
int main() {
5715+
assert(mkdir("dir", 0755) == 0);
57215716
touch("dir/a");
57225717
touch("dir/b");
57235718
touch("dir/c");
@@ -5727,26 +5722,25 @@ def test_readdir_r_silly(self):
57275722
ls("dir");
57285723
std::cout << endl;
57295724
// Attempt to delete entries as we walk the (single) directory.
5730-
::DIR * const dir = ::opendir("dir");
5731-
check(dir != NULL);
5732-
struct ::dirent *entry;
5733-
while((entry = ::readdir(dir)) != NULL) {
5734-
const std::string name(entry->d_name);
5725+
DIR* dir = opendir("dir");
5726+
assert(dir != NULL);
5727+
struct dirent *entry;
5728+
while ((entry = readdir(dir)) != NULL) {
5729+
std::string name(entry->d_name);
57355730
// Skip "." and "..".
5736-
if(name == "." || name == "..") {
5731+
if (name == "." || name == "..") {
57375732
continue;
57385733
}
57395734
// Unlink it.
57405735
std::cout << "Unlinking " << name << endl;
5741-
check(::unlink(("dir/" + name).c_str()) != -1);
5736+
assert(unlink(("dir/" + name).c_str()) != -1);
57425737
}
5743-
check(::closedir(dir) != -1);
5738+
assert(closedir(dir) != -1);
57445739
std::cout << "After:" << endl;
57455740
ls("dir");
5746-
std::cout << endl;
5741+
std::cout << "done" << endl;
57475742
return 0;
5748-
}
5749-
''')
5743+
}''')
57505744
# cannot symlink nonexistents
57515745
self.do_runf('src.cpp', r'''Before:
57525746
dir
@@ -5763,6 +5757,7 @@ def test_readdir_r_silly(self):
57635757
Unlinking e
57645758
After:
57655759
dir
5760+
done
57665761
''', args=['', 'abc'])
57675762

57685763
def test_emversion(self):

0 commit comments

Comments
 (0)