-
Notifications
You must be signed in to change notification settings - Fork 510
Add flat files to conformance tests #7853
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
kwxm
wants to merge
14
commits into
master
Choose a base branch
from
kwxm/conformance/add-flat-files
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 all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e71acfe
Add flat files
kwxm c95c43e
Add some more flat files
kwxm 61920a7
WIP
kwxm 43f8708
Change error message for failed decoding
kwxm b00f17e
Rename budget files
kwxm 409f9a6
WIP
kwxm 71542c2
WIP
kwxm 57a714a
Merge branch 'master' into kwxm/conformance/add-flat-files
kwxm 0687244
Tidying up
kwxm c9dcc6f
Update README.md
kwxm 4fd0db7
Update README.md
kwxm b1ed677
Update README.md again
kwxm c0b70a0
Remove spurious file
kwxm 2cb1764
Fix 'Invalid option --no-create' in the conformance test suites
zeme-wana File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
The diff you're trying to view is too large. We only load the first 3000 changed files.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,167 @@ | ||
| {-| Tests checking that `.flat` files decode to the same AST as the | ||
| corresponding textual `.uplc` files and that every `.uplc` file has a | ||
| corresponding `.flat` file, and vice-versa. This will make sure that | ||
| we remember to add both formats when we add a new test. -} | ||
| module Main (main) where | ||
|
|
||
| import Data.ByteString qualified as BS | ||
| import Data.Text.IO qualified as T | ||
| import PlutusConformance.Common | ||
| import PlutusPrelude (display) | ||
| import System.Directory | ||
| import System.FilePath | ||
| ( dropExtension | ||
| , takeBaseName | ||
| , (<.>) | ||
| , (</>) | ||
| ) | ||
| import Test.Tasty (defaultMain, testGroup) | ||
| import Test.Tasty.Golden (findByExtension) | ||
| import Test.Tasty.HUnit (Assertion, assertFailure, testCase) | ||
| import Test.Tasty.Providers (TestTree) | ||
| import Witherable (Witherable (wither)) | ||
|
|
||
| {-| A list of directories which should be skipped because for one reason or another | ||
| we don't have flat files or don't currently expect the test to pass. -} | ||
| skippedConsistencyTests :: [FilePath] | ||
| skippedConsistencyTests = | ||
| [ -- The tests in `constant` are supposed to test that the textual parser | ||
| -- parses constants correctly. This doesn't really make sense for `flat` | ||
| -- files, so we skip them. | ||
| "test-cases/uplc/evaluation/builtin/parser" | ||
| , "test-cases/uplc/evaluation/term/parser" | ||
| , -- We skip this test for the time being. It involves a program with a free | ||
| -- variable, and this will not be detected by the parser but will be detected | ||
| -- by the flat decoder. It's OK in the main conformance tests because free | ||
| -- variables are detected by deBruijnTerm, which we call before executing the | ||
| -- textual test cases. | ||
| "test-cases/uplc/evaluation/term/var" | ||
| ] | ||
|
|
||
| {-| Check that a `.flat` (or `.flat.expected`) file decodes to the same AST as | ||
| the textual UPLC program in the corresponding `.uplc` (or `.uplc.expected`) | ||
| file. A `.uplc.expected` file may instead contain one of the special | ||
| "failure" markers (see `expectedToProg`); in that case, and only in that case, | ||
| we expect the `.flat.expected` file to fail to decode as well (for example | ||
| because it's empty). -} | ||
| assertFlatMatchesUplc | ||
| :: FilePath | ||
| -- ^ path to the `.uplc`/`.uplc.expected` file | ||
| -> FilePath | ||
| -- ^ path to the corresponding `.flat`/`.flat.expected` file | ||
| -> Assertion | ||
| assertFlatMatchesUplc uplcFile flatFile = do | ||
| uplcResult <- expectedToProg <$> T.readFile uplcFile | ||
| flatResult <- decodeFlatProg <$> BS.readFile flatFile | ||
| case (uplcResult, flatResult) of | ||
| (Left _, Left _) -> pure () -- both fail to represent a program: fine | ||
| (Right uplcProg, Right flatProg) | ||
| | uplcProg == flatProg -> pure () | ||
| | otherwise -> | ||
| assertFailure $ | ||
| "AST mismatch between " | ||
| <> flatFile | ||
| <> " and " | ||
| <> uplcFile | ||
| <> ".\nDecoded from " | ||
| <> flatFile | ||
| <> ":\n" | ||
| <> display flatProg | ||
| <> "\nParsed from " | ||
| <> uplcFile | ||
| <> ":\n" | ||
| <> display uplcProg | ||
| (Left _, Right flatProg) -> | ||
| assertFailure $ | ||
| uplcFile | ||
| <> " does not represent a program, but " | ||
| <> flatFile | ||
| <> " decoded successfully to:\n" | ||
| <> display flatProg | ||
| (Right uplcProg, Left err) -> | ||
| assertFailure $ | ||
| flatFile | ||
| <> " failed to decode (" | ||
| <> err | ||
| <> "), but " | ||
| <> uplcFile | ||
| <> " represents the program:\n" | ||
| <> display uplcProg | ||
|
|
||
| {-| Walk a file tree (expected to be `test-cases/uplc/evaluation`). Any | ||
| directory (leaf or non-leaf) listed in `skippedDirs` is skipped entirely, | ||
| along with everything below it. For every other test-case directory, this | ||
| requires that a `.uplc` file and a `.flat` file exist together or not at | ||
| all: if one is present without the other, that's reported as a failing test | ||
| (the idea being that this should remind us to add a `.flat` file whenever we | ||
| add a new `.uplc` file, and vice versa). If both are present, we check that | ||
| the `.flat` file decodes to the same AST as the `.uplc` file, and likewise | ||
| for the `.flat.expected`/`.uplc.expected` pair. -} | ||
| discoverTestcases | ||
| :: [FilePath] | ||
| {-^ Paths, relative to the root of plutus-conformance, of directories to | ||
| skip entirely (along with any subdirectories), eg | ||
| "test-cases/uplc/evaluation/builtin/semantics/addInteger/addInteger-01" or | ||
| "test-cases/uplc/evaluation/builtin/constant". -} | ||
| -> FilePath | ||
| -- ^ The directory to search for tests. | ||
| -> IO TestTree | ||
| discoverTestcases skippedDirs = go | ||
| where | ||
| go dir | ||
| | dir `elem` skippedDirs = pure $ testGroup (takeBaseName dir) [] | ||
| | otherwise = do | ||
| let name = takeBaseName dir | ||
| children <- listDirectory dir | ||
| subdirs <- flip wither children $ \child -> do | ||
| let fullPath = dir </> child | ||
| isDir <- doesDirectoryExist fullPath | ||
| pure $ if isDir then Just fullPath else Nothing | ||
| if null subdirs | ||
| then testGroup name <$> leafTests dir | ||
| else testGroup name <$> traverse go subdirs | ||
| leafTests dir = do | ||
| uplcFiles <- findByExtension [".uplc"] dir | ||
| flatFiles <- findByExtension [".flat"] dir | ||
| uplcFile <- case uplcFiles of | ||
| [] -> pure Nothing | ||
| [f] -> pure (Just f) | ||
| _ -> error $ "More than one .uplc file in " <> dir | ||
| flatFile <- case flatFiles of | ||
| [] -> pure Nothing | ||
| [f] -> pure (Just f) | ||
| _ -> error $ "More than one .flat file in " <> dir | ||
| pure $ case (uplcFile, flatFile) of | ||
| (Nothing, Nothing) -> [] -- not a uplc/flat test-case directory | ||
| (Just u, Just f) -> | ||
| [ testCase "flat matches uplc" $ assertFlatMatchesUplc u f | ||
| , testCase "flat.expected matches uplc.expected" $ | ||
| assertFlatMatchesUplc (u <.> "expected") (f <.> "expected") | ||
| ] | ||
| (Just u, Nothing) -> | ||
| [ testCase "flat file exists" . assertFailure $ | ||
| "Missing " | ||
| <> dropExtension u <.> "flat" | ||
| <> ": every .uplc file must have a corresponding .flat file" | ||
| ] | ||
| (Nothing, Just f) -> | ||
| [ testCase "uplc file exists" . assertFailure $ | ||
| "Missing " | ||
| <> dropExtension f <.> "uplc" | ||
| <> ": every .flat file must have a corresponding .uplc file" | ||
| ] | ||
|
|
||
| {-| Run the tests that check that every `.uplc` file (outside `skippedDirs`) | ||
| has a corresponding `.flat` file (and vice versa), and that `.flat` files | ||
| decode to the same AST as their corresponding `.uplc` files. -} | ||
| runConsistencyTests | ||
| :: [FilePath] | ||
| {-^ Paths, relative to the root of plutus-conformance, of test-case | ||
| directories to skip entirely. -} | ||
| -> IO () | ||
| runConsistencyTests skippedDirs = do | ||
| tests <- discoverTestcases skippedDirs "test-cases" | ||
| defaultMain $ testGroup "Flat/UPLC decoding tests" [tests] | ||
|
|
||
| main :: IO () | ||
| main = runConsistencyTests skippedConsistencyTests |
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
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DecodeError?