Skip to content

Commit

Permalink
Merge pull request #122 from AndyTWF/config-full-paths
Browse files Browse the repository at this point in the history
fix(config): consider the full config path when parsing config files
  • Loading branch information
AndyTWF authored Jan 30, 2021
2 parents dbce234 + c7b45a7 commit c44b37b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
11 changes: 7 additions & 4 deletions src/Compiler/Config/ConfigFileLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,26 @@ public ConfigInclusionRules LoadConfigFiles(List<string> files, CompilerArgument

foreach (string file in files)
{
// Get the full path to the config file
string fullPath = Path.GetFullPath(file);

// Parse the config file as JSON
JObject jsonConfig;
try
{
jsonConfig = JObject.Parse(File.ReadAllText(file));
jsonConfig = JObject.Parse(File.ReadAllText(fullPath));
}
catch (Newtonsoft.Json.JsonReaderException e)
{
throw new ConfigFileInvalidException("Invalid JSON in " + file + ": " + e.Message);
throw new ConfigFileInvalidException("Invalid JSON in " + fullPath + ": " + e.Message);
}
catch (FileNotFoundException)
{
throw new ConfigFileInvalidException("Config file not found");
}

includeLoader.LoadConfig(config, jsonConfig, file);
optionsLoader.LoadOptions(arguments, jsonConfig, file);
includeLoader.LoadConfig(config, jsonConfig, fullPath);
optionsLoader.LoadOptions(arguments, jsonConfig, fullPath);
}

return config;
Expand Down
14 changes: 7 additions & 7 deletions tests/CompilerTest/Config/ConfigFileLoaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ public class ConfigFileLoaderTest

public ConfigFileLoaderTest()
{
this.fileLoader = ConfigFileLoaderFactory.Make();
this.arguments = new CompilerArguments();
fileLoader = ConfigFileLoaderFactory.Make();
arguments = new CompilerArguments();
}

[Theory]
[InlineData("xyz", "Config file not found")]
[InlineData("_TestData/ConfigFileLoader/InvalidJson/config.json", "Invalid JSON in _TestData/ConfigFileLoader/InvalidJson/config.json: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.")]
[InlineData("_TestData/ConfigFileLoader/NotObject/config.json", "Invalid JSON in _TestData/ConfigFileLoader/NotObject/config.json: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.")]
[InlineData("_TestData/ConfigFileLoader/InvalidJson/config.json", "Invalid JSON in .*?: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray\\. Path '', line 1, position 1\\.")]
[InlineData("_TestData/ConfigFileLoader/NotObject/config.json", "Invalid JSON in .*?: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray\\. Path '', line 1, position 1\\.")]
public void TestItThrowsExceptionOnBadData(string fileToLoad, string expectedMessage)
{
ConfigFileInvalidException exception = Assert.Throws<ConfigFileInvalidException>(
() => fileLoader.LoadConfigFiles(new List<string> {fileToLoad}, this.arguments)
() => fileLoader.LoadConfigFiles(new List<string> {fileToLoad}, arguments)
);
Assert.Equal(expectedMessage, exception.Message);
Assert.Matches(expectedMessage, exception.Message);
}

private string GetFullFilePath(string relative)
Expand All @@ -42,7 +42,7 @@ private string GetFullFilePath(string relative)
public void TestItLoadsAConfigFile()
{
ConfigInclusionRules rules = fileLoader.LoadConfigFiles(
new List<string> {"_TestData/ConfigFileLoader/ValidConfig/config.json"}, this.arguments
new List<string> {"_TestData/ConfigFileLoader/ValidConfig/config.json"}, arguments
);

List<IInclusionRule> ruleList = rules.ToList();
Expand Down

0 comments on commit c44b37b

Please sign in to comment.