Skip to content

Commit

Permalink
Added necessary mv and rm commands on Windows
Browse files Browse the repository at this point in the history
Fixed the tests
  • Loading branch information
MikaelMayer committed Aug 28, 2024
1 parent 80e7fc3 commit 73f2e43
Show file tree
Hide file tree
Showing 17 changed files with 199 additions and 18 deletions.
4 changes: 4 additions & 0 deletions Source/IntegrationTests/LitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ IEnumerable<string> AddExtraArgs(IEnumerable<string> args, IEnumerable<string> l
"%diff", (args, config) => DiffCommand.Parse(args.ToArray())
}, {
"%sed", (args, config) => SedCommand.Parse(args.ToArray())
}, {
"%mv", (args, config) => MvCommand.Parse(args.ToArray())
}, {
"%rm", (args, config) => RmCommand.Parse(args.ToArray())
}, {
"%OutputCheck", OutputCheckCommand.Parse
}
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// RUN: %baredafny translate py --python-module-name=PythonModule2 --library="%S/PythonModule1.doo" --translation-record "%S/PythonModule1-py.dtr" --output "%S/PythonModule2" "%s"
// RUN: rm -rf "%S/PythonModule2"
// RUN: mv "%S/PythonModule2-py" "%S/PythonModule2"
// RUN: %baredafny translate py --python-module-name=PythonModule2 --library="%S/PythonModule1.doo" --translation-record "%S/PythonModule1-py.dtr" --output "%S/PythonModule2" "%s" --include-runtime
// RUN: %exits-with -any %rm -rf "%S/PythonModule2"
// RUN: %mv "%S/PythonModule2-py" "%S/PythonModule2"
// RUN: pip3 install "%S"
// RUN: python3 %S/PythonModule2/ > %t
// RUN: %S/python3 PythonModule2/ > %t
// RUN: %diff "%s.expect" "%t"
module DafnyModule3 {
import DafnyModule1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
file_format_version = "1.0"
dafny_version = "4.7.0.0"
dafny_version = "4.8.0.0"
[options_by_module.DafnyModule1]
python-module-name = "PythonModule1"
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// This file is used to regenerate PythonModule1.doo
// RUN: echo 'lit should ignore this file'

module DafnyModule1 {
method HelloWorld()
{
print "Hello World";
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
file_format_version = "1.0"
dafny_version = "4.7.0.0"
dafny_version = "4.8.0.0"
[options_by_module."Some.Nested.Module"]
python-module-name = "SomeNestedModule"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: pip3 install "%S"
// RUN: %baredafny translate py --output "%S/SomeTestModule" --library="%S/SomeNestedModule.doo" --translation-record "%S/SomeNestedModule/SomeNestedModule-py.dtr" "%s"
// RUN: python3 %S/SomeTestModule-py/ > %t
// RUN: %baredafny translate py --output "%S/SomeTestModule" --library="%S/SomeNestedModule.doo" --translation-record "%S/SomeNestedModule/SomeNestedModule-py.dtr" "%s" --include-runtime
// RUN: %S/python3 SomeTestModule-py/ > %t
// RUN: %diff "%s.expect" "%t"
module SomeTestModule {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// This file is used to regenerate SomeNestedModule.doo
// RUN: echo 'lit should ignore this file'

module Some.Nested.Module {
method HelloWorld()
{
print "Hello World";
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// RUN: %baredafny translate py --python-module-name=PythonModule1 "%s" --output "%S/PythonModule1"
// RUN: rm -rf "%S/PythonModule1"
// RUN: mv "%S/PythonModule1-py/" "%S/PythonModule1"
// RUN: %baredafny translate py --python-module-name=PythonModule1 "%s" --output "%S/PythonModule1" --include-runtime
// RUN: %exits-with -any %rm -rf "%S/PythonModule1"
// RUN: %mv "%S/PythonModule1-py/" "%S/PythonModule1"
// RUN: pip3 install "%S"
// RUN: python3 %S/PythonModule1/ > %t
// RUN: %S/python3 PythonModule1/ > %t
// RUN: %diff "%s.expect" "%t"
module DafnyModule1 {

Expand Down
18 changes: 14 additions & 4 deletions Source/XUnitExtensions/Lit/ExitCommand.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
using System.ComponentModel;
using System.IO;
using System.Threading.Tasks;

namespace XUnitExtensions.Lit;


public class ExitCommand : ILitCommand {
private readonly int expectedExitCode;
private readonly string expectedExitCode;
private readonly ILitCommand operand;

public ExitCommand(int exitCode, ILitCommand operand) {
public ExitCommand(string exitCode, ILitCommand operand) {
this.expectedExitCode = exitCode;
this.operand = operand;
}

public async Task<int> Execute(TextReader inputReader,
TextWriter outputWriter, TextWriter errorWriter) {
var exitCode = await operand.Execute(inputReader, outputWriter, errorWriter);
if (exitCode == expectedExitCode) {
var exitCode = 1;
try {
exitCode = await operand.Execute(inputReader, outputWriter, errorWriter);
} catch(Win32Exception) {
if (expectedExitCode == "-any") {
} else {
throw;
}
}

if (expectedExitCode == "-any" || exitCode.ToString() == expectedExitCode) {
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion Source/XUnitExtensions/Lit/LitRunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ private static ILitCommand ParseArguments(Token[] tokens, LitTestConfiguration c
return new NotCommand(operand);
}
if (tokens[0].Value == "%exits-with") {
var ec = Int32.Parse(tokens[1].Value);
var ec = tokens[1].Value;
var operand = ParseArguments(tokens[2..], config);
return new ExitCommand(ec, operand);
}
Expand Down
57 changes: 57 additions & 0 deletions Source/XUnitExtensions/Lit/MvCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;

namespace XUnitExtensions.Lit {
public class MvCommand : ILitCommand {

private readonly string fileOrFolder;
private readonly string destination;

private MvCommand(string fileOrFolder, string destination) {
this.fileOrFolder = fileOrFolder;
this.destination = destination;
}

public static ILitCommand Parse(string[] args) {
if (args.Length != 2) {
throw new ArgumentException($"Wrong number of arguments for mv, expected 2 but got {args.Length}");
}
var fileOrFolder = args[0];
var destination = args[1];

return new MvCommand(fileOrFolder, destination);
}

public async Task<int> Execute(TextReader inputReader,
TextWriter outputWriter, TextWriter errorWriter) {
if (File.Exists(fileOrFolder)) {
try {
File.Move(fileOrFolder, destination);
} catch (Exception e) {
await outputWriter.WriteLineAsync(e.ToString());
return 1;
}
} else if (Directory.Exists(fileOrFolder)) {
try {
Directory.Move(fileOrFolder, destination);
} catch (Exception e) {
await outputWriter.WriteLineAsync(e.ToString());
return 1;
}
} else {
throw new ArgumentException("File " + fileOrFolder + " not found");
}

return 0;
}

public override string ToString() {
return $"%mv {fileOrFolder} {destination}";
}
}
}
86 changes: 86 additions & 0 deletions Source/XUnitExtensions/Lit/RmCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;

namespace XUnitExtensions.Lit {
public class RmCommand : ILitCommand {

private readonly string options;
private readonly string fileOrFolder;

private RmCommand(string options, string fileOrFolder) {
this.options = options;
this.fileOrFolder = fileOrFolder;
}

public static ILitCommand Parse(string[] args) {
if (args.Length != 1 && args.Length != 2) {
throw new ArgumentException($"Wrong number of arguments for rm, expected 1 or 2 but got {args.Length}");
}

string fileOrFolder;
string options;
if (args.Length == 2) {
options = args[0];
fileOrFolder = args[1];
} else {
options = "";
fileOrFolder = args[0];
}

return new RmCommand(options, fileOrFolder);
}
private static void RecursiveDelete(DirectoryInfo baseDir)
{
if (!baseDir.Exists) {
return;
}

foreach (var dir in baseDir.EnumerateDirectories()) {
RecursiveDelete(dir);
}
baseDir.Delete(true);
}
public async Task<int> Execute(TextReader inputReader,
TextWriter outputWriter, TextWriter errorWriter) {
if (File.Exists(fileOrFolder)) {
try {
File.Delete(fileOrFolder);
} catch (Exception e) {
await outputWriter.WriteLineAsync(e.ToString());
return 1;
}
} else if (Directory.Exists(fileOrFolder)) {
if (Directory.EnumerateFiles(fileOrFolder).Any()) {
if (options == "-rf") {
RecursiveDelete(new DirectoryInfo(fileOrFolder));
} else {
await outputWriter.WriteLineAsync("Directory is not empty. Please supply rm with -rf to force deletion");
return 1;
}
} else {
try {
Directory.Delete(fileOrFolder);
} catch (Exception e) {
await outputWriter.WriteLineAsync(e.ToString());
return 1;
}
}
} else {
await outputWriter.WriteLineAsync($"File or folder {fileOrFolder} not found");
return 1;
}

return 0;
}

public override string ToString() {
return $"%rm {(options != "" ? options + " " : "")}{fileOrFolder}";
}
}
}
2 changes: 1 addition & 1 deletion Source/XUnitExtensions/Lit/SedCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private SedCommand(string regexp, string replaceBy, string file) {

public static ILitCommand Parse(string[] args) {
if (args.Length != 2) {
throw new ArgumentException($"Wrong number of arguments for sed: {args.Length}");
throw new ArgumentException($"Wrong number of arguments for sed, expected 2 but got {args.Length}");
}
var regexpReplace = args[0];
var delimitCharacter = regexpReplace[1];
Expand Down
6 changes: 6 additions & 0 deletions Source/XUnitExtensions/Lit/ShellLitCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public async Task<int> Execute(TextReader inputReader,
TextWriter outputWriter, TextWriter errorWriter) {
using var process = new Process();

if (shellCommand.Contains('/') && Path.GetFileName(shellCommand) is var command and "python3") {
var workingDir = Path.GetDirectoryName(shellCommand);
process.StartInfo.WorkingDirectory = workingDir;
shellCommand = command;
}

process.StartInfo.FileName = shellCommand;
foreach (var argument in Arguments) {
process.StartInfo.ArgumentList.Add(argument);
Expand Down

0 comments on commit 73f2e43

Please sign in to comment.