Skip to content

Commit

Permalink
Add more tests, rename to getDataFilePath, re-add deprecated getDataF…
Browse files Browse the repository at this point in the history
…ileNameByName

refs idaholab#29141
  • Loading branch information
loganharbour committed Nov 28, 2024
1 parent 5e979ec commit 2f65c4c
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 44 deletions.
8 changes: 4 additions & 4 deletions framework/doc/content/source/interfaces/DataFileInterface.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<your_app>/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
Expand All @@ -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<DataFileName>` 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)

Expand Down
9 changes: 8 additions & 1 deletion framework/include/interfaces/DataFileInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
45 changes: 28 additions & 17 deletions framework/src/interfaces/DataFileInterface.C
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,41 @@ DataFileInterface::getDataFileName(const std::string & param) const
"within the InputParameters.\nUse getParam<DataFileName>(\"",
param,
"\") instead.");
return _parent.parameters().get<DataFileName>(param);
return _parent.getParam<DataFileName>(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<std::string> 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();
}
Expand All @@ -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;
}
2 changes: 1 addition & 1 deletion framework/src/utils/DataFileUtils.C
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ getPath(const std::string & path,
const std::optional<std::string> & base,
const std::optional<std::string> & 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())
Expand Down
7 changes: 5 additions & 2 deletions test/src/userobjects/DataFileNameTest.C
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ DataFileNameTest::validParams()
params.addParam<std::string>("data_file_by_path", "Data file to look up by path (not param)");
params.addParam<DataFileName>("data_file_deprecated",
"Data file to look up that is loaded from the deprecated method");
params.addParam<std::string>("data_file_name_by_name",
"Data file to look up using the deprecated getDataFileNameByName");
return params;
}

Expand All @@ -41,8 +43,9 @@ DataFileNameTest::DataFileNameTest(const InputParameters & parameters)
if (isParamSetByUser("data_file"))
print_path("data_file", getParam<DataFileName>("data_file"));
if (isParamSetByUser("data_file_by_path"))
print_path("data_file_by_path",
getDataFileNameByPath(getParam<std::string>("data_file_by_path")));
print_path("data_file_by_path", getDataFilePath(getParam<std::string>("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<std::string>("data_file_name_by_name")));
}
46 changes: 27 additions & 19 deletions test/tests/misc/data_file_name/tests
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down

0 comments on commit 2f65c4c

Please sign in to comment.