Skip to content

Commit

Permalink
Merge pull request #3177 from CliMA/ne/hotfix
Browse files Browse the repository at this point in the history
Add check and informative error for empty YAML file
  • Loading branch information
nefrathenrici authored Jul 8, 2024
2 parents 7238c5e + 5f05fb7 commit e688414
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/solver/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ function AtmosConfig(

configs = map(all_config_files) do config_file
@info "Loading yaml file $config_file"
strip_help_messages(YAML.load_file(config_file))
strip_help_messages(load_yaml_file(config_file))
end
return AtmosConfig(
configs;
Expand Down
13 changes: 9 additions & 4 deletions src/solver/yaml_helper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ strip_help_message(v) = v
strip_help_messages(d) =
Dict(map(k -> Pair(k, strip_help_message(d[k])), collect(keys(d)))...)

function load_yaml_file(f)
filesize(f) == 0 && error("File $f is empty or missing.")
return YAML.load_file(f)
end

"""
default_config_dict()
default_config_dict(config_path)
Loads the default configuration from files into a Dict for use in AtmosConfig().
"""
function default_config_dict(config_file = default_config_file)
config = YAML.load_file(config_file)
config = load_yaml_file(config_file)
return strip_help_messages(config)
end

Expand Down Expand Up @@ -45,10 +50,10 @@ Takes in a Dict, vector of Dicts or filepaths and returns a Dict with the
default configuration overridden by the given dicts or parsed YAML files.
"""
override_default_config(config_files::AbstractString) =
override_default_config(YAML.load_file(config_files))
override_default_config(load_yaml_file(config_files))

override_default_config(config_files::ContainerType(AbstractString)) =
override_default_config(YAML.load_file.(config_files))
override_default_config(load_yaml_file.(config_files))

override_default_config(config_dicts::ContainerType(AbstractDict)) =
override_default_config(merge(config_dicts...))
Expand Down Expand Up @@ -121,7 +126,7 @@ function configs_per_config_id(
file = joinpath(root, f)
!endswith(file, ".yml") && continue
occursin("default_configs", file) && continue
config = YAML.load_file(file)
config = load_yaml_file(file)
name = config_id_from_config_file(file)
cmds[name] = (; config, config_file = file)
end
Expand Down
6 changes: 5 additions & 1 deletion test/config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function extract_job_ids(folder_path)
for (root, _, files) in walkdir(folder_path)
for file in files
filepath = joinpath(root, file)
data = YAML.load_file(filepath)
data = CA.load_yaml_file(filepath)
if haskey(data, "job_id")
job_id_dict[filepath] = data["job_id"]
end
Expand All @@ -33,3 +33,7 @@ end
repeated_job_ids = filter(kv -> length(kv[2]) > 1, value_to_keys)

@test isempty(repeated_job_ids)

file, io = mktemp()
config_err = ErrorException("File $(CA.normrelpath(file)) is empty or missing.")
@test_throws config_err CA.AtmosConfig(file)

0 comments on commit e688414

Please sign in to comment.