Skip to content

Commit 6f5d032

Browse files
committed
Added execution cache tests
1 parent 3c15c66 commit 6f5d032

File tree

5 files changed

+123
-23
lines changed

5 files changed

+123
-23
lines changed

src/Dotnet.Script.Core/Commands/ExecuteScriptCommand.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
22
using System.IO;
3+
using System.Linq;
34
using System.Security.Cryptography;
45
using System.Text;
56
using System.Threading.Tasks;
7+
using Dotnet.Script.DependencyModel.Environment;
68
using Dotnet.Script.DependencyModel.Logging;
79
using Dotnet.Script.DependencyModel.ProjectSystem;
810

@@ -45,10 +47,10 @@ private async Task<TReturn> DownloadAndRunCode<TReturn>(ExecuteScriptCommandOpti
4547
private string GetLibrary(ExecuteScriptCommandOptions executeOptions)
4648
{
4749
var projectFolder = FileUtils.GetPathToTempFolder(Path.GetDirectoryName(executeOptions.File.Path));
48-
var publishDirectory = Path.Combine(projectFolder, "publish");
49-
var pathToLibrary = Path.Combine(publishDirectory, "script.dll");
50+
var executionCacheFolder = Path.Combine(projectFolder, "execution-cache");
51+
var pathToLibrary = Path.Combine(executionCacheFolder, "script.dll");
5052

51-
if (!TryCreateHash(executeOptions, out var hash) || !TryGetHash(publishDirectory, out var cachedHash))
53+
if (!TryCreateHash(executeOptions, out var hash) || !TryGetHash(executionCacheFolder, out var cachedHash))
5254
{
5355
return CreateLibrary();
5456
}
@@ -62,13 +64,13 @@ private string GetLibrary(ExecuteScriptCommandOptions executeOptions)
6264

6365
string CreateLibrary()
6466
{
65-
var options = new PublishCommandOptions(executeOptions.File,publishDirectory, "script", PublishType.Library,executeOptions.OptimizationLevel, null, executeOptions.NoCache);
67+
var options = new PublishCommandOptions(executeOptions.File,executionCacheFolder, "script", PublishType.Library,executeOptions.OptimizationLevel, null, executeOptions.NoCache);
6668
new PublishCommand(_scriptConsole, _logFactory).Execute(options);
6769
if (hash != null)
6870
{
69-
File.WriteAllText(Path.Combine(publishDirectory, "script.sha256"), hash);
71+
File.WriteAllText(Path.Combine(executionCacheFolder, "script.sha256"), hash);
7072
}
71-
return Path.Combine(publishDirectory, "script.dll");
73+
return Path.Combine(executionCacheFolder, "script.dll");
7274
}
7375
}
7476

@@ -81,18 +83,18 @@ public bool TryCreateHash(ExecuteScriptCommandOptions options, out string hash)
8183
return false;
8284
}
8385

84-
var projectFolder = FileUtils.GetPathToTempFolder(Path.GetDirectoryName(options.File.Path));
85-
var pathToProjectFile = Path.Combine(projectFolder, "script.csproj");
86-
var projectFile = new ProjectFile(File.ReadAllText(pathToProjectFile));
86+
var scriptFilesProvider = new ScriptFilesResolver();
87+
var allScriptFiles = scriptFilesProvider.GetScriptFiles(options.File.Path);
88+
var projectFile = new ScriptProjectProvider(_logFactory).CreateProjectFileFromScriptFiles(ScriptEnvironment.Default.TargetFramework, allScriptFiles.ToArray());
89+
8790
if (!projectFile.IsCacheable)
8891
{
8992
_logger.Warning($"The script {options.File.Path} is not cacheable. For caching and optimal performance, ensure that the script only contains NuGet references with pinned/exact versions.");
9093
hash = null;
9194
return false;
9295
}
9396

94-
var scriptFilesProvider = new ScriptFilesResolver();
95-
var allScriptFiles = scriptFilesProvider.GetScriptFiles(options.File.Path);
97+
9698
IncrementalHash incrementalHash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256);
9799
foreach (var scriptFile in allScriptFiles)
98100
{

src/Dotnet.Script.DependencyModel/ProjectSystem/ScriptProjectProvider.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,33 @@ public string CreateProject(string targetDirectory, string defaultTargetFramewor
8282
_logger.Debug($"Creating project file for *.csx files found in {targetDirectory} using {defaultTargetFramework} as the default framework.");
8383

8484
var csxFiles = Directory.GetFiles(targetDirectory, "*.csx", SearchOption.AllDirectories);
85-
return CreateProjectFileFromScriptFiles(targetDirectory, defaultTargetFramework, csxFiles);
85+
return SaveProjectFileFromScriptFiles(targetDirectory, defaultTargetFramework, csxFiles);
8686
}
8787

8888
public string CreateProjectForScriptFile(string scriptFile)
8989
{
9090
_logger.Debug($"Creating project file for {scriptFile}");
9191
var scriptFiles = _scriptFilesResolver.GetScriptFiles(scriptFile);
92-
return CreateProjectFileFromScriptFiles(Path.GetDirectoryName(scriptFile), _scriptEnvironment.TargetFramework, scriptFiles.ToArray());
92+
return SaveProjectFileFromScriptFiles(Path.GetDirectoryName(scriptFile), _scriptEnvironment.TargetFramework, scriptFiles.ToArray());
9393
}
9494

95-
private string CreateProjectFileFromScriptFiles(string targetDirectory, string defaultTargetFramework, string[] csxFiles)
95+
private string SaveProjectFileFromScriptFiles(string targetDirectory, string defaultTargetFramework, string[] csxFiles)
9696
{
97-
var parseresult = _scriptParser.ParseFromFiles(csxFiles);
97+
ProjectFile projectFile = CreateProjectFileFromScriptFiles(defaultTargetFramework, csxFiles);
9898

9999
var pathToProjectFile = GetPathToProjectFile(targetDirectory);
100+
projectFile.Save(pathToProjectFile);
101+
102+
LogProjectFileInfo(pathToProjectFile);
103+
104+
CopyNuGetConfigFile(targetDirectory, Path.GetDirectoryName(pathToProjectFile));
105+
return pathToProjectFile;
106+
}
107+
108+
public ProjectFile CreateProjectFileFromScriptFiles(string defaultTargetFramework, string[] csxFiles)
109+
{
110+
var parseresult = _scriptParser.ParseFromFiles(csxFiles);
111+
100112
var projectFile = new ProjectFile();
101113

102114
foreach (var packageReference in parseresult.PackageReferences)
@@ -105,13 +117,7 @@ private string CreateProjectFileFromScriptFiles(string targetDirectory, string d
105117
}
106118

107119
projectFile.TargetFramework = parseresult.TargetFramework ?? defaultTargetFramework;
108-
109-
projectFile.Save(pathToProjectFile);
110-
111-
LogProjectFileInfo(pathToProjectFile);
112-
113-
CopyNuGetConfigFile(targetDirectory, Path.GetDirectoryName(pathToProjectFile));
114-
return pathToProjectFile;
120+
return projectFile;
115121
}
116122

117123
private void CopyNuGetConfigFile(string targetDirectory, string pathToProjectFileFolder)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System.IO;
2+
using Dotnet.Script.Shared.Tests;
3+
using Xunit;
4+
using Xunit.Abstractions;
5+
6+
namespace Dotnet.Script.Tests
7+
{
8+
public class ExecutionCacheTests
9+
{
10+
public ExecutionCacheTests(ITestOutputHelper testOutputHelper)
11+
{
12+
testOutputHelper.Capture();
13+
}
14+
15+
[Fact]
16+
public void ShouldNotUpdateHashWhenSourceIsNotChanged()
17+
{
18+
using (var scriptFolder = new DisposableFolder())
19+
{
20+
var pathToScript = Path.Combine(scriptFolder.Path, "main.csx");
21+
22+
WriteScript(pathToScript, "WriteLine(42);");
23+
var firstResult = Execute(pathToScript);
24+
Assert.Contains("42", firstResult.output);
25+
26+
WriteScript(pathToScript, "WriteLine(42);");
27+
var secondResult = Execute(pathToScript);
28+
Assert.Contains("42", secondResult.output);
29+
30+
Assert.Equal(firstResult.hash, secondResult.hash);
31+
}
32+
}
33+
34+
35+
[Fact]
36+
public void ShouldUpdateHashWhenSourceChanges()
37+
{
38+
using (var scriptFolder = new DisposableFolder())
39+
{
40+
var pathToScript = Path.Combine(scriptFolder.Path, "main.csx");
41+
42+
WriteScript(pathToScript, "WriteLine(42);");
43+
var firstResult = Execute(pathToScript);
44+
Assert.Contains("42", firstResult.output);
45+
46+
WriteScript(pathToScript, "WriteLine(84);");
47+
var secondResult = Execute(pathToScript);
48+
Assert.Contains("84", secondResult.output);
49+
50+
Assert.NotEqual(firstResult.hash, secondResult.hash);
51+
}
52+
}
53+
54+
[Fact]
55+
public void ShouldNotCreateHashWhenScriptIsNotCacheable()
56+
{
57+
using (var scriptFolder = new DisposableFolder())
58+
{
59+
var pathToScript = Path.Combine(scriptFolder.Path, "main.csx");
60+
61+
WriteScript(pathToScript, "#r \"nuget:AutoMapper, 7.0\"" ,"WriteLine(42);");
62+
63+
var result = Execute(pathToScript);
64+
Assert.Contains("42", result.output);
65+
66+
Assert.Null(result.hash);
67+
}
68+
}
69+
70+
71+
private static (string output, string hash) Execute(string pathToScript)
72+
{
73+
var result = ScriptTestRunner.Default.Execute(pathToScript);
74+
Assert.Equal(0, result.exitCode);
75+
76+
var pathToTempFolder = Path.GetDirectoryName(Dotnet.Script.DependencyModel.ProjectSystem.FileUtils.GetPathToTempFolder(pathToScript));
77+
var pathToExecutionCache = Path.Combine(pathToTempFolder, "execution-cache");
78+
var pathToCacheFile = Path.Combine(pathToExecutionCache, "script.sha256");
79+
string cachedhash = null;
80+
if (File.Exists(pathToCacheFile))
81+
{
82+
cachedhash = File.ReadAllText(pathToCacheFile);
83+
}
84+
return (result.output, cachedhash);
85+
}
86+
87+
private static void WriteScript(string path, params string[] lines)
88+
{
89+
File.WriteAllLines(path, lines);
90+
}
91+
}
92+
}

src/Dotnet.Script.Tests/PackageVersionTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public void ShouldBePinned(string version)
2727
[InlineData("(1.0,2.0)")]
2828
[InlineData("[1.0,2.0)")]
2929
[InlineData("(1.0)")]
30+
[InlineData("")]
3031
public void ShouldNotBePinned(string version)
3132
{
3233
Assert.False(new PackageVersion(version).IsPinned);

src/Dotnet.Script.Tests/ScriptPublisherTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public void SimplePublishTest()
3030
var code = @"WriteLine(""hello world"");";
3131
var mainPath = Path.Combine(workspaceFolder.Path, "main.csx");
3232
File.WriteAllText(mainPath, code);
33-
var test = ScriptTestRunner.Default.ExecuteInProcess($"publish {mainPath}");
3433

3534
var publishResult = ScriptTestRunner.Default.Execute($"publish {mainPath}", workspaceFolder.Path);
3635
Assert.Equal(0, publishResult.exitCode);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy