Skip to content

Commit

Permalink
Fixed go tests for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
MikaelMayer committed Aug 29, 2024
1 parent 73f2e43 commit 5faf876
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Source/IntegrationTests/LitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ IEnumerable<string> AddExtraArgs(IEnumerable<string> args, IEnumerable<string> l
"%mv", (args, config) => MvCommand.Parse(args.ToArray())
}, {
"%rm", (args, config) => RmCommand.Parse(args.ToArray())
}, {
"%cp", (args, config) => CpCommand.Parse(args.ToArray())
}, {
"%OutputCheck", OutputCheckCommand.Parse
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// RUN: %baredafny translate go --go-module-name=GoModule2 --library="%S/test.doo" --translation-record "%S/test-go.dtr" --output "%S/test3" "%s"
// RUN: cp -r "%S/go.*" "%S/test3-go/"
// RUN: cp -r "%S/../../../../../../../../../DafnyRuntime/DafnyRuntimeGo-gomod" "%S"
// RUN: cp -r "%S/DafnyModule1" "%S/test3-go/"
// RUN: %cp -rf "%S/go.mod" "%S/test3-go/go.mod"
// RUN: %cp -rf "%S/go.sum" "%S/test3-go/go.sum"
// RUN: %cp -rf "%S/../../../../../../../../../DafnyRuntime/DafnyRuntimeGo-gomod" "%S"
// RUN: %cp -rf "%S/DafnyModule1" "%S/test3-go/"
// RUN: go run -C %S/test3-go/ test3.go > %t
// RUN: %diff "%s.expect" "%t"
module DafnyModule3 {
Expand Down
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";
}
}
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]
go-module-name = "GoModule1"
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// RUN: %baredafny translate go --go-module-name=GoModule1 "%s" --output "%S/test"
// RUN: cp -r "%S/go.*" "%S/test-go/"
// RUN: cp -r "%S/../../../../../../../../../../DafnyRuntime/DafnyRuntimeGo-gomod" "%S"
// RUN: %cp -rf "%S/go.mod" "%S/test-go/go.mod"
// RUN: %cp -rf "%S/go.sum" "%S/test-go/go.sum"
// RUN: %cp -rf "%S/../../../../../../../../../../DafnyRuntime/DafnyRuntimeGo-gomod" "%S"
// RUN: go run -C %S/test-go/ test.go > %t
// RUN: %diff "%s.expect" "%t"
module DafnyModule1 {
Expand Down
Binary file not shown.
Binary file not shown.
109 changes: 109 additions & 0 deletions Source/XUnitExtensions/Lit/CpCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
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 CpCommand : ILitCommand {

private readonly string options;
private readonly string fileOrFolder;
private readonly string destination;

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

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

string fileOrFolder;
string destination;
string options;

if (args.Length == 2) {
options = "";
fileOrFolder = args[0];
destination = args[1];
} else {
options = args[0];
fileOrFolder = args[1];
destination = args[2];
}
return new CpCommand(options, fileOrFolder, destination);
}
static void CopyDirectory(string sourceDir, string destinationDir, bool recursive, bool force)
{
// Get information about the source directory
var dir = new DirectoryInfo(sourceDir);

// Check if the source directory exists
if (!dir.Exists)
throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");

// Cache directories before we start copying
DirectoryInfo[] dirs = dir.GetDirectories();

// Create the destination directory
Directory.CreateDirectory(destinationDir);

// Get the files in the source directory and copy to the destination directory
foreach (FileInfo file in dir.GetFiles())
{
string targetFilePath = Path.Combine(destinationDir, file.Name);
if (force && File.Exists(targetFilePath)) {
File.Delete(targetFilePath);
}
file.CopyTo(targetFilePath);
}

// If recursive and copying subdirectories, recursively call this method
if (recursive) {
foreach (DirectoryInfo subDir in dirs) {
string newDestinationDir = Path.Combine(destinationDir, subDir.Name);
CopyDirectory(subDir.FullName, newDestinationDir, true, force);
}
}
}
public async Task<int> Execute(TextReader inputReader,
TextWriter outputWriter, TextWriter errorWriter) {
if (File.Exists(fileOrFolder)) {
try {
if (File.Exists(destination) && options.Contains('f')) {
File.Delete(destination);
}
File.Copy(fileOrFolder, destination);
} catch (Exception e) {
await outputWriter.WriteLineAsync(e.ToString());
return 1;
}
} else if (Directory.Exists(fileOrFolder)) {
try {
var actualDestination = Directory.Exists(destination)
? Path.Combine(destination, Path.GetFileName(fileOrFolder))
: destination;
CopyDirectory(fileOrFolder, actualDestination, options.Contains('r'), options.Contains('f'));
} catch (Exception e) {
await outputWriter.WriteLineAsync(e.ToString());
return 1;
}
} else {
throw new ArgumentException("File or folder " + fileOrFolder + " not found");
}

return 0;
}

public override string ToString() {
return $"%cp {(options != "" ? options + " " : "")}{fileOrFolder} {destination}";
}
}
}
2 changes: 1 addition & 1 deletion Source/XUnitExtensions/Lit/MvCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public async Task<int> Execute(TextReader inputReader,
return 1;
}
} else {
throw new ArgumentException("File " + fileOrFolder + " not found");
throw new ArgumentException("File or folder " + fileOrFolder + " not found");
}

return 0;
Expand Down

0 comments on commit 5faf876

Please sign in to comment.