-
Notifications
You must be signed in to change notification settings - Fork 94
Fix struct dirent d_type macro test #348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sjshuck
wants to merge
4
commits into
haskell:master
Choose a base branch
from
sjshuck:fix-dirent-d-type
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* The purpose of this C program is to do one of 4 things. Which one it does, | ||
* indicates to the DirEnt test suite something it ought to do. | ||
* | ||
* fail to compile | ||
* This means d_type is undefined. It should be enough to test for | ||
* HAVE_STRUCT_DIRENT_D_TYPE, but this test suite came about because of | ||
* https://github.com/haskell/unix/issues/347 which was caused by a | ||
* misspelling of that macro, so we're testing it another way. | ||
* Absence of d_type means the test suite ought to skip testing for non- | ||
* DT_UNKNOWN values for d_type, since dirEntType returns UnknownType in this | ||
* case. | ||
* exit with status 0 | ||
* This means the "." entry in the DIR stream opened at ".", guaranteed to be | ||
* a directory, has a d_type of DT_DIR. We should proceed with a test for it | ||
* in Haskell. | ||
* exit with status 1 | ||
* This means something unexpected went wrong. Fail the Haskell test also. | ||
* exit with stauts 2 | ||
* This means the "." entry has a d_type of DT_UNKNOWN. This is valid; no | ||
* filesystem or operating system is required to yield a useful d_type. | ||
* We should skip testing for non-DT_UNKNOWN values in Haskell. | ||
sjshuck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
|
||
#include <dirent.h> | ||
#include <errno.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <sys/types.h> | ||
|
||
void check_error(const char *msg) { | ||
if (errno) { | ||
perror(msg); | ||
exit(1); | ||
} | ||
} | ||
|
||
int main() { | ||
sjshuck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
printf("Testing struct dirent d_type in C\n"); | ||
|
||
DIR *dir = opendir("."); | ||
check_error("opendir"); | ||
|
||
struct dirent *de = NULL; | ||
|
||
do { | ||
de = readdir(dir); | ||
check_error("readdir"); | ||
} while (de && strcmp(de->d_name, ".") != 0); | ||
// We found the . dir or encountered end of dir stream | ||
|
||
int status = 0; | ||
|
||
if (!de) { | ||
printf("Read the whole . dir without encountering \".\"!\n"); | ||
status = 1; | ||
} else if (de->d_type == DT_DIR) { | ||
printf("Got DT_DIR for d_type for \".\"\n"); | ||
} else if (de->d_type == DT_UNKNOWN) { | ||
printf("Got DT_UNKNOWN for d_type for \".\"\n"); | ||
// Signal that we should skip test for non-zero d_type | ||
status = 2; | ||
} else { | ||
printf("Got %d for d_type for \".\"!\n", (int)de->d_type); | ||
status = 1; | ||
} | ||
|
||
closedir(dir); | ||
check_error("closedir"); | ||
|
||
exit(status); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
|
||
module Main (main) where | ||
|
||
import Control.Exception (bracket, finally) | ||
import Foreign.C.String (peekCString) | ||
import System.Exit | ||
import System.Posix.Directory | ||
import System.Posix.Directory.Internals | ||
import System.Process (system) | ||
|
||
system_x :: String -> IO ExitCode | ||
system_x cmd = system $ "set -x; " ++ cmd | ||
|
||
onFailure :: IO ExitCode -> (ExitCode -> IO ()) -> IO () | ||
action `onFailure` after = action >>= \case | ||
ExitSuccess -> return () | ||
ec -> after ec | ||
infixr 9 `onFailure` | ||
|
||
prepareTest :: IO () | ||
prepareTest = do | ||
system_x "cc --version" `onFailure` exitWith | ||
sjshuck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
system_x "[ -f tests/DirEnt.c ]" `onFailure` \ec -> do | ||
putStrLn "Not running tests from root of repo?" | ||
exitWith ec | ||
system_x "cc tests/DirEnt.c -o DirEnt-test" `onFailure` \_ -> do | ||
putStrLn "d_type not available? Skipping Haskell test" | ||
exitSuccess | ||
-- As written, this C code exits with 2 if it determines the Haskell test | ||
-- for broken dirEntType will be a false positive | ||
system_x "./DirEnt-test" `onFailure` \case | ||
ExitFailure 2 -> putStrLn "Skipping Haskell test" >> exitSuccess | ||
ec -> exitWith ec | ||
|
||
peekDirEnt :: DirEnt -> IO (String, DirType) | ||
peekDirEnt dirEnt = do | ||
dName <- dirEntName dirEnt >>= peekCString | ||
dType <- dirEntType dirEnt | ||
return (dName, dType) | ||
|
||
testDirTypeOfDot :: DirStream -> IO () | ||
testDirTypeOfDot dirStream = go where | ||
go = readDirStreamWith peekDirEnt dirStream >>= \case | ||
Just (".", DirectoryType) -> do | ||
putStrLn "Got DirectoryType for . dir" | ||
exitSuccess | ||
Just (".", dType) -> die $ "Got " ++ show dType ++ " for . dir!" | ||
Just _ -> go | ||
Nothing -> die "Read cwd in Haskell and didn't find . dir!" | ||
|
||
main :: IO () | ||
main = do | ||
putStrLn "Preparing Haskell test of dirEntType" | ||
prepareTest `finally` system_x "rm -f DirEnt-test" | ||
|
||
putStrLn "Running Haskell test of dirEntType" | ||
bracket (openDirStream ".") closeDirStream testDirTypeOfDot |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.