Add Change Tree test

This commit is contained in:
code liturgy 2022-11-14 21:12:18 +00:00
parent dafb489eb4
commit 47c7c3acf6
2 changed files with 22 additions and 6 deletions

View File

@ -7,8 +7,8 @@ public class AugeasTests
{ {
private Augeas _augeas; private Augeas _augeas;
private const string EXAMPLE_CONF_1 = "/etc/apache2/sites-available/example.com.conf"; private const string ExampleConf1 = "/etc/apache2/sites-available/example.com.conf";
private const string EXAMPLE_CONF_2 = "/etc/apache2/sites-available/example2.com.conf"; private const string ExampleConf2 = "/etc/apache2/sites-available/example2.com.conf";
private static string RandomString(int length) private static string RandomString(int length)
{ {
@ -24,21 +24,32 @@ public class AugeasTests
var rootDir = Environment.CurrentDirectory + "/root"; var rootDir = Environment.CurrentDirectory + "/root";
var lensDir = Environment.CurrentDirectory + "/lens"; var lensDir = Environment.CurrentDirectory + "/lens";
_augeas = new Augeas(new AugSettings(rootDir, lensDir)); _augeas = new Augeas(new AugSettings(rootDir, lensDir));
_augeas.LoadFile(EXAMPLE_CONF_1); _augeas.LoadFile(ExampleConf1);
_augeas.LoadFile(EXAMPLE_CONF_2); _augeas.LoadFile(ExampleConf2);
} }
[Test] [Test]
public void GetTreeHasDirectivesAndArguments() public void GetTreeHasDirectivesAndArguments()
{ {
var tree = _augeas.GetTree("VirtualHost", $"/files{EXAMPLE_CONF_1}/VirtualHost/*"); var tree = _augeas.GetTree("VirtualHost", $"/files{ExampleConf1}/VirtualHost/*");
Assert.That(tree.Arguments.Count > 0 && tree.Directives.Count > 0); Assert.That(tree.Arguments.Count > 0 && tree.Directives.Count > 0);
} }
[Test]
public void TreeIsChangeable()
{
var tree = _augeas.GetTree("VirtualHost", $"/files{ExampleConf1}/VirtualHost/*");
var directive = tree.GetDirective("ServerName");
var newValue = RandomString(10);
directive.Set($"ServerName {newValue}");
bool save = directive.Save();
Assert.That(save);
}
[Test] [Test]
public void NoExceptionThrownWhenPrintingPreview() public void NoExceptionThrownWhenPrintingPreview()
{ {
var virtualHostConfig = EXAMPLE_CONF_1; var virtualHostConfig = ExampleConf1;
_augeas.PrintPreview(virtualHostConfig); _augeas.PrintPreview(virtualHostConfig);
Assert.Pass(); Assert.Pass();
} }
@ -107,4 +118,5 @@ public class AugeasTests
Assert.That(sites.Length > 0); Assert.That(sites.Length > 0);
} }
} }

View File

@ -20,6 +20,10 @@ public class Node
Path = path; Path = path;
} }
/// <summary>
/// Write all pending changes to disk. Only files that had any changes made to them are written.
/// </summary>
/// <returns></returns>
public bool Save() public bool Save()
{ {
return _augeas.Save(); return _augeas.Save();