Skip to content

Commit

Permalink
Updated convention to work updated wildcard trimming in XmlConfigTran…
Browse files Browse the repository at this point in the history
…sformDefinition.
  • Loading branch information
Rob Pearson committed Dec 4, 2015
1 parent 04312b7 commit bfd86ea
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -11,7 +10,6 @@
using Calamari.Tests.Helpers;
using NSubstitute;
using NUnit.Framework;
using Octostache;

namespace Calamari.Tests.Fixtures.Conventions
{
Expand All @@ -22,7 +20,7 @@ public class ConfigurationTransformConventionFixture
IConfigurationTransformer configurationTransformer;
RunningDeployment deployment;
CalamariVariableDictionary variables;
const string stagingDirectory = "c:\\applications\\acme\\1.0.0";
const string StagingDirectory = "c:\\applications\\acme\\1.0.0";

[SetUp]
public void SetUp()
Expand All @@ -31,18 +29,18 @@ public void SetUp()
configurationTransformer = Substitute.For<IConfigurationTransformer>();

variables = new CalamariVariableDictionary();
variables.Set(SpecialVariables.OriginalPackageDirectoryPath, stagingDirectory);
variables.Set(SpecialVariables.OriginalPackageDirectoryPath, StagingDirectory);

deployment = new RunningDeployment("C:\\packages", variables);
}

[Test]
public void ShouldApplyReleaseTransformIfAutomaticallyRunConfigurationTransformationFilesFlagIsSet()
{
var webConfig = Path.Combine(stagingDirectory, "web.config");
var webConfigReleaseTransform = Path.Combine(stagingDirectory, "web.Release.config");
var webConfig = Path.Combine(StagingDirectory, "web.config");
var webConfigReleaseTransform = Path.Combine(StagingDirectory, "web.Release.config");

MockSearchableFiles(fileSystem, stagingDirectory, new[] { webConfig, webConfigReleaseTransform }, "*.config");
MockSearchableFiles(fileSystem, StagingDirectory, new[] { webConfig, webConfigReleaseTransform }, "*.config");

variables.Set(SpecialVariables.Package.AutomaticallyRunConfigurationTransformationFiles, true.ToString());
CreateConvention().Install(deployment);
Expand All @@ -53,10 +51,10 @@ public void ShouldApplyReleaseTransformIfAutomaticallyRunConfigurationTransforma
[Test]
public void ShouldNotApplyReleaseTransformIfAutomaticallyRunConfigurationTransformationFilesFlagNotSet()
{
var webConfig = Path.Combine(stagingDirectory, "web.config");
var webConfigReleaseTransform = Path.Combine(stagingDirectory, "web.Release.config");
var webConfig = Path.Combine(StagingDirectory, "web.config");
var webConfigReleaseTransform = Path.Combine(StagingDirectory, "web.Release.config");

MockSearchableFiles(fileSystem, stagingDirectory, new[] { webConfig, webConfigReleaseTransform }, "*.config");
MockSearchableFiles(fileSystem, StagingDirectory, new[] { webConfig, webConfigReleaseTransform }, "*.config");

variables.Set(SpecialVariables.Package.AutomaticallyRunConfigurationTransformationFiles, false.ToString());
CreateConvention().Install(deployment);
Expand All @@ -68,10 +66,10 @@ public void ShouldNotApplyReleaseTransformIfAutomaticallyRunConfigurationTransfo
public void ShouldApplyEnvironmentTransform()
{
const string environment = "Production";
var webConfig = Path.Combine(stagingDirectory, "web.config");
var environmentTransform = Path.Combine(stagingDirectory, "web.Production.config");
var webConfig = Path.Combine(StagingDirectory, "web.config");
var environmentTransform = Path.Combine(StagingDirectory, "web.Production.config");

MockSearchableFiles(fileSystem, stagingDirectory, new[] { webConfig, environmentTransform }, "*.config");
MockSearchableFiles(fileSystem, StagingDirectory, new[] { webConfig, environmentTransform }, "*.config");

variables.Set(SpecialVariables.Package.AutomaticallyRunConfigurationTransformationFiles, true.ToString());
variables.Set(SpecialVariables.Environment.Name, environment);
Expand All @@ -83,10 +81,10 @@ public void ShouldApplyEnvironmentTransform()
[Test]
public void ShouldApplySpecificCustomTransform()
{
var webConfig = Path.Combine(stagingDirectory, "web.config");
var specificTransform = Path.Combine(stagingDirectory, "web.Foo.config");
var webConfig = Path.Combine(StagingDirectory, "web.config");
var specificTransform = Path.Combine(StagingDirectory, "web.Foo.config");

MockSearchableFiles(fileSystem, stagingDirectory, new[] { webConfig, specificTransform }, "*.config");
MockSearchableFiles(fileSystem, StagingDirectory, new[] { webConfig, specificTransform }, "*.config");

variables.Set(SpecialVariables.Package.AdditionalXmlConfigurationTransforms, "web.Foo.config => web.config");
// This will be applied even if the automatically run flag is set to false
Expand All @@ -98,10 +96,10 @@ public void ShouldApplySpecificCustomTransform()

[Test]
[TestCaseSource(nameof(AdvancedTransformTestCases))]
public void ShouldApplyAdvancedTransformations(string sourceFile, string transformDefinition,
string expectedAppliedTransform)
public void ShouldApplyAdvancedTransformations(string sourceFile, string transformDefinition, string expectedAppliedTransform)
{
MockSearchableFiles(fileSystem, stagingDirectory, new[] { sourceFile, expectedAppliedTransform }, "*" + Path.GetExtension(sourceFile));
var searchPattern = "*" + Path.GetExtension(sourceFile);
MockSearchableFiles(fileSystem, StagingDirectory, new[] { sourceFile, expectedAppliedTransform }, searchPattern);

variables.Set(SpecialVariables.Package.AdditionalXmlConfigurationTransforms, transformDefinition);
// This will be applied even if the automatically run flag is set to false
Expand All @@ -114,10 +112,10 @@ public void ShouldApplyAdvancedTransformations(string sourceFile, string transfo
[Test]
public void ShouldNotApplyAdvancedTransformationsInSubFolderWithWildcard()
{
var webConfig = Path.Combine(stagingDirectory, "web.config");
var specificTransform = Path.Combine(stagingDirectory, "config\\web.Foo.config");
var webConfig = Path.Combine(StagingDirectory, "web.config");
var specificTransform = Path.Combine(StagingDirectory, "config\\web.Foo.config");

MockSearchableFiles(fileSystem, stagingDirectory, new[] { webConfig, specificTransform }, "*.config");
MockSearchableFiles(fileSystem, StagingDirectory, new[] { webConfig, specificTransform }, "*.config");

variables.Set(SpecialVariables.Package.AdditionalXmlConfigurationTransforms, "config\\*.Foo.config => web.config");
// This will be applied even if the automatically run flag is set to false
Expand All @@ -127,6 +125,32 @@ public void ShouldNotApplyAdvancedTransformationsInSubFolderWithWildcard()
configurationTransformer.DidNotReceive().PerformTransform(webConfig, specificTransform, webConfig);
}

[Test]
public void ShouldApplyAdvancedTransformationToAppropriateFilesAndNotOtherTransformationFiles()
{
var newDoc = Path.Combine(StagingDirectory, "New Text Document.txt");
var octoExe = Path.Combine(StagingDirectory, "Octo.exe");
var octoExeConfig = Path.Combine(StagingDirectory, "Octo.exe.config");
var somethingConfig = Path.Combine(StagingDirectory, "Something.config");
var somethingReleaseConfig = Path.Combine(StagingDirectory, "Something.Release.config");
var somethingReleaseXml = Path.Combine(StagingDirectory, "Something.Release.xml");
var somethingTestConfig = Path.Combine(StagingDirectory, "Something.Test.config");
var somethingTestXml = Path.Combine(StagingDirectory, "Something.Test.xml");
var somethingXml = Path.Combine(StagingDirectory, "Something.xml");
var webConfig = Path.Combine(StagingDirectory, "Web.config");
var webReleaseConfig = Path.Combine(StagingDirectory, "Web.Release.config");

MockSearchableFiles(fileSystem, StagingDirectory, new[] { newDoc, octoExe, octoExeConfig, somethingConfig, somethingReleaseConfig, somethingReleaseXml, somethingTestConfig, somethingTestXml, somethingXml, webConfig, webReleaseConfig }, "*.xml");

variables.Set(SpecialVariables.Package.AdditionalXmlConfigurationTransforms, "*.Release.xml => *.xml");
variables.Set(SpecialVariables.Package.AutomaticallyRunConfigurationTransformationFiles, false.ToString());
CreateConvention().Install(deployment);

configurationTransformer.Received().PerformTransform(somethingXml, somethingReleaseXml, somethingXml);
configurationTransformer.DidNotReceive().PerformTransform(somethingTestXml, somethingReleaseXml, somethingTestXml);
configurationTransformer.ReceivedWithAnyArgs(1).PerformTransform("", "", ""); // Only Called Once
}

private static IEnumerable AdvancedTransformTestCases
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,15 @@ public void Install(RunningDeployment deployment)
}

var transformsRun = new HashSet<string>();
foreach (var configFile in fileSystem.EnumerateFilesRecursively(deployment.CurrentDirectory,
sourceExtensions.ToArray()))
foreach (var configFile in fileSystem.EnumerateFilesRecursively(deployment.CurrentDirectory, sourceExtensions.ToArray()))
{
ApplyTransformations(configFile, transformDefinitions, transformsRun);

}

deployment.Variables.SetStrings(SpecialVariables.AppliedXmlConfigTransforms, transformsRun, "|");
}

void ApplyTransformations(string sourceFile, IEnumerable<XmlConfigTransformDefinition> transformations,
HashSet<string> alreadyRun)
void ApplyTransformations(string sourceFile, IEnumerable<XmlConfigTransformDefinition> transformations, HashSet<string> alreadyRun)
{
foreach (var transformation in transformations)
{
Expand All @@ -63,7 +60,6 @@ void ApplyTransformations(string sourceFile, IEnumerable<XmlConfigTransformDefin

if ((transformation.Wildcard && !sourceFile.EndsWith(transformation.SourcePattern, StringComparison.InvariantCultureIgnoreCase)))
continue;

try
{
ApplyTransformations(sourceFile, transformation, alreadyRun);
Expand Down Expand Up @@ -133,7 +129,13 @@ private static string DetermineTransformFileName(string sourceFile, XmlConfigTra

if (transformation.Advanced && transformation.Wildcard)
{
var baseFileName = sourceFile.Replace(transformation.SourcePattern, "");
var sourcePatternWithoutPrefix = transformation.SourcePattern;
if (transformation.SourcePattern.StartsWith("."))
{
sourcePatternWithoutPrefix = transformation.SourcePattern.Remove(0, 1);
}

var baseFileName = sourceFile.Replace(sourcePatternWithoutPrefix, "");
return Path.ChangeExtension(baseFileName, tp);
}

Expand All @@ -142,7 +144,5 @@ private static string DetermineTransformFileName(string sourceFile, XmlConfigTra

return Path.ChangeExtension(sourceFile, tp);
}


}
}

1 comment on commit bfd86ea

@robpearson
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.