Guard against NullReference while creating HostContext (#2343)

* Guard against NullReference while creating HostContext

* Throw on IOUtil loading object if content is empty or object is null

* removed unused dependency

* Changed exceptions to ArgumentNullException and ArgumentException

* fixed my mistake on LoadObject

* fixed tests

* trigger workflows

* trigger workflows

* encode files using utf8
This commit is contained in:
Nikola Jokic
2023-03-30 15:39:09 +02:00
committed by GitHub
parent 9f778b814d
commit 1ceb1a67f2
3 changed files with 43 additions and 5 deletions

View File

@@ -40,10 +40,19 @@ namespace GitHub.Runner.Sdk
File.WriteAllText(path, StringUtil.ConvertToJson(obj), Encoding.UTF8);
}
public static T LoadObject<T>(string path)
public static T LoadObject<T>(string path, bool required = false)
{
string json = File.ReadAllText(path, Encoding.UTF8);
return StringUtil.ConvertFromJson<T>(json);
if (required && string.IsNullOrEmpty(json))
{
throw new ArgumentNullException($"File {path} is empty");
}
T result = StringUtil.ConvertFromJson<T>(json);
if (required && result == null)
{
throw new ArgumentException("Converting json to object resulted in a null value");
}
return result;
}
public static string GetSha256Hash(string path)