From 2f65c4c5cf45ced7d7d808dca2d23c9e62c7ce7b Mon Sep 17 00:00:00 2001 From: Logan Harbour Date: Wed, 27 Nov 2024 19:50:31 -0700 Subject: [PATCH] Add more tests, rename to getDataFilePath, re-add deprecated getDataFileNameByName refs #29141 --- .../source/interfaces/DataFileInterface.md | 8 ++-- .../include/interfaces/DataFileInterface.h | 9 +++- framework/src/interfaces/DataFileInterface.C | 45 +++++++++++------- framework/src/utils/DataFileUtils.C | 2 +- test/src/userobjects/DataFileNameTest.C | 7 ++- test/tests/misc/data_file_name/tests | 46 +++++++++++-------- 6 files changed, 73 insertions(+), 44 deletions(-) diff --git a/framework/doc/content/source/interfaces/DataFileInterface.md b/framework/doc/content/source/interfaces/DataFileInterface.md index bdde9a065653..79ddfc767e2e 100644 --- a/framework/doc/content/source/interfaces/DataFileInterface.md +++ b/framework/doc/content/source/interfaces/DataFileInterface.md @@ -5,7 +5,7 @@ MOOSE based apps and modules. | Method | Description | | - | - | -getDataFileNameByPath | Finds a data file given a relative path +getDataFilePath | Finds a data file given a relative path Files located in `/data` can be registered as data paths for use in installed and in-tree builds of applications. The MOOSE framework and MOOSE module data directories @@ -22,11 +22,11 @@ parameters. This search is consistent between both in-tree and installed builds When a parameter is specified as `DataFileName` type, the corresponding value that you get via `getParam` is the searched value (the user's input is used for the search). -You may also utilize the `getDataFileNameByPath()` method within this interface to manually +You may also utilize the `getDataFilePath()` method within this interface to manually search for a relative path in the data without the use of a parameter (for hard-coded data). The search order for these path is the following: -- if the path is absolute, use the absolute path -- relative to the input file (only for `DataFileName` parameters, not `getDataFileNameByPath()`) +- if the path is absolute, use the absolute path (only for `DataFileName` parameters) +- relative to the input file (only for `DataFileName` parameters) - relative to all installed and registered data file directories (for an installed application) - relative to all in-tree registered data file directories (for an in-tree build) diff --git a/framework/include/interfaces/DataFileInterface.h b/framework/include/interfaces/DataFileInterface.h index 98091c8c10d9..826379692c92 100644 --- a/framework/include/interfaces/DataFileInterface.h +++ b/framework/include/interfaces/DataFileInterface.h @@ -40,12 +40,19 @@ class DataFileInterface */ std::string getDataFileName(const std::string & param) const; + /** + * Deprecated method. + * + * Use getDataFilePath() instead. + */ + std::string getDataFileNameByName(const std::string & relative_path) const; + /** * Returns the path of a data file for a given relative file path. * This can be used for hardcoded datafile names and will search the same locations * as getDataFileName */ - std::string getDataFileNameByPath(const std::string & path) const; + std::string getDataFilePath(const std::string & relative_path) const; private: const ParallelParamObject & _parent; diff --git a/framework/src/interfaces/DataFileInterface.C b/framework/src/interfaces/DataFileInterface.C index 6c9d12d286d3..c445c6b08bdf 100644 --- a/framework/src/interfaces/DataFileInterface.C +++ b/framework/src/interfaces/DataFileInterface.C @@ -23,24 +23,41 @@ DataFileInterface::getDataFileName(const std::string & param) const "within the InputParameters.\nUse getParam(\"", param, "\") instead."); - return _parent.parameters().get(param); + return _parent.getParam(param); } std::string -DataFileInterface::getDataFileNameByPath(const std::string & path) const +DataFileInterface::getDataFileNameByName(const std::string & relative_path) const { + _parent.mooseDeprecated("getDataFileNameByName() is deprecated. Use getDataFilePath(\"", + relative_path, + "\") instead."); + return getDataFilePath(relative_path); +} + +std::string +DataFileInterface::getDataFilePath(const std::string & relative_path) const +{ + // This should only ever be used with relative paths. There is no point to + // use this search path with an absolute path. + if (std::filesystem::path(relative_path).is_absolute()) + _parent.mooseError("While using getDataFilePath(\"", + relative_path, + "\"): This API should not be used for absolute paths."); + // Throw on error so that if getPath() fails, we can throw an error // with the context of _parent.mooseError() const auto throw_on_error_before = Moose::_throw_on_error; Moose::_throw_on_error = true; std::optional error; + // This will search the data paths for this relative path Moose::DataFileUtils::Path found_path; try { - found_path = Moose::DataFileUtils::getPath(path); + found_path = Moose::DataFileUtils::getPath(relative_path); } - catch (MooseException & e) + catch (std::exception & e) { error = e.what(); } @@ -49,19 +66,13 @@ DataFileInterface::getDataFileNameByPath(const std::string & path) const if (error) _parent.mooseError(*error); - if (found_path.context == Moose::DataFileUtils::Context::DATA) - { - mooseAssert(found_path.data_name, "Should be set"); - const std::string msg = - "Using data file '" + found_path.path + "' from " + *found_path.data_name + " data"; - _parent.mooseInfo(msg); - } - else - { - mooseAssert(found_path.context == Moose::DataFileUtils::Context::ABSOLUTE, - "Missing branch and relative should not be hit"); - mooseAssert(!found_path.data_name, "Should not be set"); - } + mooseAssert(found_path.context == Moose::DataFileUtils::Context::DATA, + "Should only ever obtain data"); + mooseAssert(found_path.data_name, "Should be set"); + + const std::string msg = + "Using data file '" + found_path.path + "' from " + *found_path.data_name + " data"; + _parent.mooseInfo(msg); return found_path.path; } diff --git a/framework/src/utils/DataFileUtils.C b/framework/src/utils/DataFileUtils.C index 5f2b14f1c2e6..af35dbb274eb 100644 --- a/framework/src/utils/DataFileUtils.C +++ b/framework/src/utils/DataFileUtils.C @@ -22,7 +22,7 @@ getPath(const std::string & path, const std::optional & base, const std::optional & data_name) { - const std::filesystem::path value_path = std::filesystem::path(std::string(path)); + const std::filesystem::path value_path = std::filesystem::path(path); // File is absolute, no need to search if (std::filesystem::path(path).is_absolute()) diff --git a/test/src/userobjects/DataFileNameTest.C b/test/src/userobjects/DataFileNameTest.C index 519b66e74f1e..27eeb31e19b8 100644 --- a/test/src/userobjects/DataFileNameTest.C +++ b/test/src/userobjects/DataFileNameTest.C @@ -24,6 +24,8 @@ DataFileNameTest::validParams() params.addParam("data_file_by_path", "Data file to look up by path (not param)"); params.addParam("data_file_deprecated", "Data file to look up that is loaded from the deprecated method"); + params.addParam("data_file_name_by_name", + "Data file to look up using the deprecated getDataFileNameByName"); return params; } @@ -41,8 +43,9 @@ DataFileNameTest::DataFileNameTest(const InputParameters & parameters) if (isParamSetByUser("data_file")) print_path("data_file", getParam("data_file")); if (isParamSetByUser("data_file_by_path")) - print_path("data_file_by_path", - getDataFileNameByPath(getParam("data_file_by_path"))); + print_path("data_file_by_path", getDataFilePath(getParam("data_file_by_path"))); if (isParamSetByUser("data_file_deprecated")) print_path("data_file_deprecated", getDataFileName("data_file_deprecated")); + if (isParamSetByUser("data_file_name_by_name")) + print_path("data_file_name_by_name", getDataFileNameByName(getParam("data_file_name_by_name"))); } diff --git a/test/tests/misc/data_file_name/tests b/test/tests/misc/data_file_name/tests index d9f513497d79..11e58810da33 100644 --- a/test/tests/misc/data_file_name/tests +++ b/test/tests/misc/data_file_name/tests @@ -22,7 +22,7 @@ installation_type = relocated [] [] - [param_error] + [param_not_found] type = RunException input = test.i cli_args = 'UserObjects/data_file/data_file=this/file/does/not/exist/anywhere.dat' @@ -48,25 +48,33 @@ installation_type = relocated [] [] + [by_path_not_found] + type = RunException + input = test.i + cli_args = 'UserObjects/data_file/data_file_by_path=surely/no/exist' + expect_err = "The following error occurred in the UserObject 'data_file' of type DataFileNameTest.\s+Unable to find the data file 'surely/no/exist\' anywhere" + requirement = 'The system shall throw an exception if finding a data file via a path in designated directories fails' + [] + [by_path_absolute] + type = RunException + input = test.i + cli_args = 'UserObjects/data_file/data_file_by_path=/path/to/README.md' + expect_err = 'While using getDataFilePath\("/path/to/README.md"\): This API should not be used for absolute paths.' + requirement = 'The system shall show an exception when searching data files in designated directories by path with an absolute path' + [] [param_deprecated] - requirement = 'The system shall be to find data files via a parameter in designated directories using the deprecated interface for' - [in_tree] - type = RunApp - input = test.i - cli_args = 'UserObjects/data_file/data_file_deprecated=README.md' - expect_out = 'data_file_deprecated_relative="../../../../framework/data/README.md"' - detail = 'in-tree builds' - installation_type = in_tree - [] - [relocated] - type = RunApp - input = test.i - cli_args = 'UserObjects/data_file/data_file=README.md' - expect_out = data_file_deprecated_relative_to_binary="../share/moose/data/README.md"' - requirement = 'The system shall be to find data files via a parameter in designated directories for installed builds' - detail = 'and in installed builds' - installation_type = relocated - [] + type = RunApp + input = test.i + cli_args = 'UserObjects/data_file/data_file_deprecated=README.md' + expect_out = 'data_file_deprecated_relative="(.*)/data/README.md"' + requirement = 'The system shall be to find data files via a parameter in designated directories using the deprecated interface' + [] + [by_name_deprecated] + type = RunApp + input = test.i + cli_args = 'UserObjects/data_file/data_file_name_by_name=README.md' + expect_out = 'data_file_name_by_name_relative="(.*)/data/README.md"' + requirement = 'The system shall be to find data files in designated directories by relative path using the deprecated interface' [] [show_data_files] type = RunApp