Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions APSIM.Shared/Utilities/StringUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -983,5 +983,22 @@ public static string GetLine(string text, int lineNo)
}
return currentLine;
}

/// <summary>
/// Convert an object to a string.
/// If the object is a DateTime then it is converted
/// to a string using the invariant culture.
/// </summary>
/// <param name="obj">The object to convert.</param>
/// <returns>A string representation of the object.</returns>
public static string ConvertObjectToString(object obj)
{
if (obj == null)
return string.Empty;
else if (obj is DateTime dt)
return DateUtilities.GetDateAsString(dt);
else
return obj.ToString();
}
}
}
17 changes: 15 additions & 2 deletions Models/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,20 +256,33 @@ private static void DoCommands(Options options, string[] files, string commandFi
using var streamReader = new StreamReader(options.Batch);
var dataTable = DataTableUtilities.FromCSV(options.Batch, streamReader.ReadToEnd());

Console.WriteLine($"Running batch commands for {dataTable.Rows.Count} rows");
foreach (DataRow row in dataTable.Rows)
{
Console.WriteLine($"Running batch command {dataTable.Rows.IndexOf(row) + 1}/{dataTable.Rows.Count}.");
List<string> commandsList = File.ReadAllLines(commandFileName).ToList();

var dict = row.Table.Columns
.Cast<DataColumn>()
.ToDictionary(c => c.ColumnName, c => row[c].ToString());
.ToDictionary(c => c.ColumnName, c => StringUtilities.ConvertObjectToString(row[c]));

for (int i = 0; i < commandsList.Count; i++)
commandsList[i] = Macro.Replace(commandsList[i], dict);

foreach (string file in files)
ExecuteCommands(options, commandsList, file, relativeToDirectory, row);
{
try
{
ExecuteCommands(options, commandsList, file, relativeToDirectory, row);
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred when trying to run the batch command\n" + ex.ToString());
exitCode++;
}
}
}
Console.WriteLine($"Finished running batch commands for {dataTable.Rows.Count} rows with {exitCode} error(s).");
}
}
else
Expand Down
31 changes: 31 additions & 0 deletions Tests/UnitTests/CommandLineArgsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;

namespace UnitTests
{
Expand Down Expand Up @@ -1304,6 +1305,36 @@ public void TestApplySwitch_HandlesDateTimeStrings_WithOddFormatting()
Assert.That(clockNodeAfterChange.Start, Is.EqualTo(new DateTime(2017, 2, 1)));
}

/// <summary>
/// Test to make sure that when running a batch file with the apply
/// switch, if one of the rows has an error,
/// the other rows will still run and produce output files.
/// </summary>
[Test]
public static void TestBatch_Continues_OnError()
{
string batchFilePath = Path.Combine(Path.GetTempPath(), "batch.csv");
string commandFilePath = Path.Combine(Path.GetTempPath(), "commands.txt");
string testSimFilePath = Path.Combine(Path.GetTempPath(), "test.apsimx");
string weatherFilePath = Path.Combine(Path.GetTempPath(), "AU_Dalby.met");
string batchFileContent =
"Date,\n" +
"djnejfnjkenfkjenfj,\n" +
"1900-01-02,\n";
string commandFileContent =
$"load test.apsimx\n" +
$"[Clock].StartDate=$Date\n" +
$"save test_$Date.apsimx\n" +
$"run";
File.WriteAllText(batchFilePath, batchFileContent);
File.WriteAllText(commandFilePath, commandFileContent);
File.WriteAllText(testSimFilePath, ReflectionUtilities.GetResourceAsString("UnitTests.Resources.BatchTestResources.test.apsimx"));
File.WriteAllText(weatherFilePath, ReflectionUtilities.GetResourceAsString("UnitTests.Resources.BatchTestResources.AU_Dalby.met"));
Assert.Throws<Exception>(() => Utilities.RunModels($"--apply {commandFilePath} --batch {batchFilePath}"));
Assert.That(File.Exists(Path.Combine(Path.GetTempPath(), "test_1900-01-02.apsimx")), Is.True);
}



}
}
Loading
Loading