Skip to content

Commit

Permalink
Create output directory if missing (#23)
Browse files Browse the repository at this point in the history
This fixes issue #18 where TREATASCONTENT will fail if the output directory is missing.
  • Loading branch information
lbuesching committed Jan 2, 2024
1 parent 3bd0a51 commit 0109c39
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Sage.Engine.Tests/Functions/ContentTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2022, salesforce.com, inc.
// All rights reserved.
// SPDX-License-Identifier: Apache-2.0
// For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/Apache-2.0

using Sage.Engine.Runtime;

namespace Sage.Engine.Tests.Functions
{
[TestFixture]
internal class ContentTests : FunctionTestBase
{
[Test]
[TestCase("%%=ADD(1,2)=%%", "3")]
public void TestTreatAsContentNoOutputDirectory(string input, string expected)
{
var options = TestCompilationOptions();
options.OutputDirectory = new DirectoryInfo(Path.Combine(options.OutputDirectory.FullName, "doesnotexist"));
var context = new RuntimeContext(_serviceProvider, options);

string results = context.TREATASCONTENT(input);
Assert.That(results, Is.EqualTo(expected));
}
}
}
6 changes: 6 additions & 0 deletions src/Sage.Engine/Compiler/CompilerOptionsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public CompilerOptionsBuilder WithInputFile(FileInfo inputFile)
public CompilerOptionsBuilder WithSourceCode(string name, string sourceCode)
{
this.InputName = name;

if (!this.OutputDirectory.Exists)
{
this.OutputDirectory.Create();
}

this.InputFile = new FileInfo(Path.Combine(this.OutputDirectory.FullName, name + ".generated.ampscript"));
File.WriteAllText(InputFile.FullName, this.SourceCode);
this.SourceCode = sourceCode;
Expand Down

0 comments on commit 0109c39

Please sign in to comment.