Skip to content

Commit 1a8e96b

Browse files
committed
Avoid elm-test-rs command failing on many environments
Remove the dependency on the additional setup of the environment before running `elm-test-rs`: Remove the dependency on node. For discussion around this functionality, see: + mpizenberg/elm-test-rs#37 + mpizenberg/elm-test-rs#92
1 parent 89f1700 commit 1a8e96b

File tree

1 file changed

+65
-10
lines changed

1 file changed

+65
-10
lines changed

implement/elm-fullstack/ElmTestRs.cs

+65-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using Pine;
1+
using Mono.Unix;
2+
using Pine;
23
using System.Collections.Generic;
34
using System.Collections.Immutable;
5+
using System.IO;
46
using System.Linq;
57
using System.Runtime.InteropServices;
68

@@ -29,11 +31,25 @@ public class ElmTestRs
2931
("98708c4ecd1a34b61545f0faca7ac3c46b5bb3d0bf6b4719b16eb4cffe0a82ac",
3032
@"https://github.com/mpizenberg/elm-test-rs/releases/download/v1.2.2/elm-test-rs_windows.zip"));
3133

32-
static public byte[] LoadElmTestRsExecutableFileForCurrentOs()
34+
static public IReadOnlyDictionary<OSPlatform, (string hash, string remoteSource)> DenoExecutableFileByOs =
35+
ImmutableDictionary<OSPlatform, (string hash, string remoteSource)>.Empty
36+
.Add(
37+
OSPlatform.Linux,
38+
("34374d77515b093028df6605b6e74985aa6cd7a022c5e8310a14d65daecbd91d",
39+
@"https://github.com/denoland/deno/releases/download/v1.16.2/deno-x86_64-unknown-linux-gnu.zip"))
40+
.Add(
41+
OSPlatform.Windows,
42+
("3a1611329306d24b24fd6b98418ddec9ee79c044623e712093db24eba6a573df",
43+
@"https://github.com/denoland/deno/releases/download/v1.16.2/deno-x86_64-pc-windows-msvc.zip"));
44+
45+
static public byte[] ElmTestRsExecutableFileForCurrentOs() => LoadFileForCurrentOs(ElmTestRsExecutableFileByOs);
46+
47+
static public byte[] DenoExecutableFileForCurrentOs() => LoadFileForCurrentOs(DenoExecutableFileByOs);
48+
49+
static public byte[] LoadFileForCurrentOs(IReadOnlyDictionary<OSPlatform, (string hash, string remoteSource)> dict)
3350
{
3451
var hashAndRemoteSource =
35-
ElmTestRsExecutableFileByOs
36-
.FirstOrDefault(c => RuntimeInformation.IsOSPlatform(c.Key)).Value;
52+
dict.FirstOrDefault(c => RuntimeInformation.IsOSPlatform(c.Key)).Value;
3753

3854
if (hashAndRemoteSource.hash == null)
3955
throw new System.Exception("Unknown OS: " + RuntimeInformation.OSDescription);
@@ -48,22 +64,61 @@ static public byte[] LoadElmTestRsExecutableFileForCurrentOs()
4864
static public (string stdout, string stderr, IReadOnlyList<(string rawLine, ElmTestRsReportJsonEntry parsedLine)> stdoutLines) Run(
4965
IImmutableDictionary<IImmutableList<string>, IReadOnlyList<byte>> elmProjectFiles)
5066
{
51-
var elmTestExecutableFile = LoadElmTestRsExecutableFileForCurrentOs();
67+
var elmTestExecutableFile = ElmTestRsExecutableFileForCurrentOs();
5268

5369
var elmExecutableFileName = "elm.exe";
5470

71+
string elmTestEnvironmentPath = null;
72+
73+
{
74+
/*
75+
* We found no way yet to point elm-test-rs to the deno executable file.
76+
* As a temporary solution, adapt the file path and environment variables to help elm-test-rs find it.
77+
* */
78+
79+
var (denoExecutableFileName, pathEnvironmentVarSeparator) =
80+
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ("deno.exe", ";") : ("deno", ":");
81+
82+
var denoExecutableFileContent = DenoExecutableFileForCurrentOs();
83+
84+
var denoExecutableFileDirectory =
85+
Path.Combine(Filesystem.CacheDirectory,
86+
"bin-by-sha256",
87+
CommonConversion.StringBase16FromByteArray(CommonConversion.HashSHA256(denoExecutableFileContent)));
88+
89+
var denoExecutableFilePath = Path.Combine(denoExecutableFileDirectory, denoExecutableFileName);
90+
91+
Directory.CreateDirectory(denoExecutableFileDirectory);
92+
93+
File.WriteAllBytes(denoExecutableFilePath, denoExecutableFileContent);
94+
95+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
96+
{
97+
var unixFileInfo = new UnixFileInfo(denoExecutableFilePath);
98+
99+
unixFileInfo.FileAccessPermissions |=
100+
FileAccessPermissions.GroupExecute | FileAccessPermissions.UserExecute | FileAccessPermissions.OtherExecute |
101+
FileAccessPermissions.GroupRead | FileAccessPermissions.UserRead | FileAccessPermissions.OtherRead;
102+
}
103+
104+
var environmentPath =
105+
System.Environment.GetEnvironmentVariable("PATH") ?? System.Environment.GetEnvironmentVariable("path");
106+
107+
elmTestEnvironmentPath = environmentPath + pathEnvironmentVarSeparator + denoExecutableFileDirectory;
108+
}
109+
55110
var environmentFilesExecutable =
56-
ImmutableDictionary.Create<IImmutableList<string>, IReadOnlyList<byte>>().SetItem(
57-
ImmutableList.Create(elmExecutableFileName), ElmFullstack.ProcessFromElm019Code.GetElmExecutableFile);
111+
ImmutableDictionary.Create<IImmutableList<string>, IReadOnlyList<byte>>()
112+
.SetItem(ImmutableList.Create(elmExecutableFileName), ElmFullstack.ProcessFromElm019Code.GetElmExecutableFile);
58113

59114
var executeElmTestResult =
60115
ExecutableFile.ExecuteFileWithArguments(
61116
environmentFilesNotExecutable: elmProjectFiles,
62117
executableFile: elmTestExecutableFile,
63-
arguments: "--compiler=./" + elmExecutableFileName + " --report=json",
64-
environmentStrings: null,
118+
arguments: "--compiler=./" + elmExecutableFileName + " --deno --report=json",
119+
environmentStrings: ImmutableDictionary<string, string>.Empty.SetItem("PATH", elmTestEnvironmentPath),
65120
workingDirectory: null,
66-
environmentFilesExecutable: environmentFilesExecutable);
121+
environmentFilesExecutable: environmentFilesExecutable); ;
67122

68123
var stdoutLines =
69124
executeElmTestResult.processOutput.StandardOutput

0 commit comments

Comments
 (0)