Skip to content

Commit

Permalink
Merge #1127 - "Catch GUIconfig.xml parsing error correctly" into master
Browse files Browse the repository at this point in the history
* commit '1e33ebd':
  Configuration tests: tidy up tempdir at end of each test.
  Add tests for the LoadConfiguration method
  Catch XmlException too as it's the exception thrown by Mono
  Add more information in the error message of Configuration.LoadConfiguration
  Catch the correct type of exception in LoadConfiguration
  • Loading branch information
pjf committed Jun 21, 2015
2 parents d655652 + 1e33ebd commit 100afbd
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
20 changes: 18 additions & 2 deletions GUI/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,25 @@ public static Configuration LoadConfiguration(string path)
{
configuration = (Configuration) serializer.Deserialize(stream);
}
catch(System.Xml.XmlException)
catch (System.Exception e)
{
string message = string.Format("Error trying to parse \"{0}\". Try to move it out of the folder and restart CKAN.", path);
string additionalErrorData = "";

if(e is System.InvalidOperationException) // Exception thrown in Windows / .NET
{
if(e.InnerException != null)
additionalErrorData = ": " + e.InnerException.Message;
}
else if(e is System.Xml.XmlException) // Exception thrown in Mono
{
additionalErrorData = ": " + e.Message;
}
else
{
throw;
}

string message = string.Format("Error trying to parse \"{0}\"{1}. Try to move it out of the folder and restart CKAN.", path, additionalErrorData);
throw new Kraken(message);
}
}
Expand Down
22 changes: 22 additions & 0 deletions Tests/Data/TestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,28 @@ public static void CopyDirectory(string src, string dst)
File.Copy(file, file.Replace(src, dst));
}
}

public static string ConfigurationFile()
{
return @"<?xml version=""1.0"" encoding=""utf-8""?>
<Configuration xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
<CommandLineArguments>KSP.exe -force-opengl</CommandLineArguments>
<AutoCloseWaitDialog>false</AutoCloseWaitDialog>
<URLHandlerNoNag>false</URLHandlerNoNag>
<CheckForUpdatesOnLaunch>true</CheckForUpdatesOnLaunch>
<CheckForUpdatesOnLaunchNoNag>true</CheckForUpdatesOnLaunchNoNag>
<SortByColumnIndex>2</SortByColumnIndex>
<SortDescending>false</SortDescending>
<WindowSize>
<Width>1024</Width>
<Height>664</Height>
</WindowSize>
<WindowLoc>
<X>512</X>
<Y>136</Y>
</WindowLoc>
</Configuration>";
}
}

public class RandomModuleGenerator
Expand Down
53 changes: 53 additions & 0 deletions Tests/GUI/Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.IO;
using CKAN;
using NUnit.Framework;
using Tests.Data;

namespace Tests.GUI
{
[TestFixture]
public class ConfigurationTests
{
string tempDir;

[TestFixtureSetUp]
public void Setup()
{
tempDir = TestData.NewTempDir();
}

[TestFixtureTearDown]
public void TearDown()
{
Directory.Delete(tempDir, true);
}

[Test]
public void LoadConfiguration_MalformedXMLFile_ThrowsKraken()
{
string tempFile = Path.Combine(tempDir, "invalid.xml");

using (var stream = new StreamWriter(tempFile))
{
stream.Write("This is not a valid XML file.");
}

Assert.Throws<Kraken>(() => Configuration.LoadConfiguration(tempFile));
}

[Test]
public void LoadConfiguration_CorrectConfigurationFile_Loaded()
{
string tempFile = Path.Combine(tempDir, "valid.xml");

using (var stream = new StreamWriter(tempFile))
{
stream.Write(TestData.ConfigurationFile());
}

var result = Configuration.LoadConfiguration(tempFile);

Assert.IsNotNull(result);
}
}
}
4 changes: 4 additions & 0 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
<Compile Include="Data\DisposableKSP.cs" />
<Compile Include="Data\TestData.cs" />
<Compile Include="Data\ZipLib.cs" />
<Compile Include="GUI\Configuration.cs" />
<Compile Include="GUI\GUIMod.cs" />
<Compile Include="GUI\MainModList.cs" />
<Compile Include="NetKAN\AVC.cs" />
Expand Down Expand Up @@ -166,6 +167,9 @@
<Name>CKAN-netkan</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSHARP.Targets" />
<ProjectExtensions>
<VisualStudio AllowExistingFolder="true" />
Expand Down

0 comments on commit 100afbd

Please sign in to comment.