Esempio n. 1
0
        public void ShouldRunCsxScriptDirectly()
        {
            using (var scriptFolder = new DisposableFolder())
            {
                Directory.CreateDirectory(scriptFolder.Path);
                var(output, exitCode) = ScriptTestRunner.Default.Execute("init", scriptFolder.Path);

                var scriptPath = Path.Combine(scriptFolder.Path, "main.csx");

                if (ScriptEnvironment.Default.IsWindows)
                {
                    (output, exitCode) = ProcessHelper.RunAndCaptureOutput("cmd.exe", $"/c \"{scriptPath}\"", scriptFolder.Path);
                    Assert.Equal("Hello world!", output.Trim());
                }
                else
                {
                    // this depends on dotnet-script being installed as a dotnet global tool because the shebang needs to
                    // point to an executable in the environment.  If you have dotnet-script installed as a global tool this
                    // test will pass
                    (output, exitCode) = ProcessHelper.RunAndCaptureOutput("dotnet-script", $"-h", scriptFolder.Path);
                    if (exitCode == 0)
                    {
                        (output, exitCode) = ProcessHelper.RunAndCaptureOutput(scriptPath, "");
                        Assert.Equal("Hello world!", output.Trim());
                    }
                }
            }
        }
        public void ShouldThrowExceptionOnRestoreError()
        {
            using (var projectFolder = new DisposableFolder())
            {
                var pathToProjectFile = Path.Combine(projectFolder.Path, "script.csproj");

                var projectFile = new ProjectFile();
                projectFile.PackageReferences.Add(ValidPackageReferenceA);
                projectFile.PackageReferences.Add(InvalidPackageReferenceA);
                projectFile.PackageReferences.Add(ValidPackageReferenceB);
                projectFile.Save(pathToProjectFile);

                var projectFileInfo = new ProjectFileInfo(pathToProjectFile, string.Empty);

                var logFactory    = TestOutputHelper.CreateTestLogFactory();
                var commandRunner = new CommandRunner(logFactory);
                var restorer      = new DotnetRestorer(commandRunner, logFactory);

                var exception = Assert.Throws <Exception>(() =>
                {
                    restorer.Restore(projectFileInfo, Array.Empty <string>());
                });

                Assert.Contains("NU1101", exception.Message); // unable to find package
            }
        }
Esempio n. 3
0
        public void SimplePublishTest()
        {
            using (var workspaceFolder = new DisposableFolder())
            {
                var code     = @"WriteLine(""hello world"");";
                var mainPath = Path.Combine(workspaceFolder.Path, "main.csx");
                File.WriteAllText(mainPath, code);

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

                var exePath             = Path.Combine(workspaceFolder.Path, "publish", _scriptEnvironment.RuntimeIdentifier, "main");
                var executableRunResult = _commandRunner.Execute(exePath);

                Assert.Equal(0, executableRunResult);

                var publishedFiles = Directory.EnumerateFiles(Path.Combine(workspaceFolder.Path, "publish", _scriptEnvironment.RuntimeIdentifier));
                if (_scriptEnvironment.NetCoreVersion.Major >= 3)
                {
                    Assert.True(publishedFiles.Count() == 1, "There should be only a single published file");
                }
                else
                {
                    Assert.True(publishedFiles.Count() > 1, "There should be multiple published files");
                }
            }
        }
Esempio n. 4
0
        public void ShouldThrowMeaningfulErrorMessageWhenDependencyIsNotFound()
        {
            using (var libraryFolder = new DisposableFolder())
            {
                // Create a package that we can reference

                ProcessHelper.RunAndCaptureOutput("dotnet", "new classlib -n SampleLibrary", libraryFolder.Path);
                ProcessHelper.RunAndCaptureOutput("dotnet", "pack", libraryFolder.Path);

                using (var scriptFolder = new DisposableFolder())
                {
                    var code = new StringBuilder();
                    code.AppendLine("#r \"nuget:SampleLibrary, 1.0.0\"");
                    code.AppendLine("WriteLine(42)");
                    var pathToScript = Path.Combine(scriptFolder.Path, "main.csx");
                    File.WriteAllText(pathToScript, code.ToString());

                    // Run once to ensure that it is cached.
                    var result = ScriptTestRunner.Default.Execute(pathToScript);
                    Assert.Contains("42", result.output);

                    // Remove the package from the global NuGet cache
                    TestPathUtils.RemovePackageFromGlobalNugetCache("SampleLibrary");

                    result = ScriptTestRunner.Default.Execute(pathToScript);
                    Assert.Contains("Try executing/publishing the script", result.output);

                    // Run again with the '--no-cache' option to assert that the advice actually worked.
                    result = ScriptTestRunner.Default.Execute($"{pathToScript} --no-cache");
                    Assert.Contains("42", result.output);
                }
            }
        }
        public void ShouldRestoreProjectPackageReferences()
        {
            using (var projectFolder = new DisposableFolder())
            {
                var pathToProjectFile = Path.Combine(projectFolder.Path, "script.csproj");

                var projectFile = new ProjectFile();
                projectFile.PackageReferences.Add(ValidPackageReferenceA);
                projectFile.PackageReferences.Add(ValidPackageReferenceB);
                projectFile.Save(pathToProjectFile);

                var projectFileInfo = new ProjectFileInfo(pathToProjectFile, string.Empty);

                var logFactory    = TestOutputHelper.CreateTestLogFactory();
                var commandRunner = new CommandRunner(logFactory);
                var restorer      = new DotnetRestorer(commandRunner, logFactory);

                var pathToProjectObjDirectory = Path.Combine(projectFolder.Path, "obj");

                Assert.False(Directory.Exists(pathToProjectObjDirectory));

                restorer.Restore(projectFileInfo, Array.Empty <string>());

                Assert.True(Directory.Exists(pathToProjectObjDirectory));
            }
        }
        public void ShouldThrowMeaningfulExceptionWhenScriptPackageIsMissing()
        {
            using (var scriptFolder = new DisposableFolder())
            {
                var code = new StringBuilder();
                code.AppendLine("#load \"nuget:ScriptPackageWithMainCsx, 1.0.0\"");
                code.AppendLine("SayHello();");
                var pathToScriptFile = Path.Combine(scriptFolder.Path, "main.csx");
                File.WriteAllText(pathToScriptFile, code.ToString());
                var pathToScriptPackages = Path.GetFullPath(ScriptPackagesFixture.GetPathToPackagesFolder());

                // Run once to ensure that it is cached.
                var result = ScriptTestRunner.Default.Execute($"{pathToScriptFile} -s {pathToScriptPackages}");
                Assert.StartsWith("Hello from netstandard2.0", result.output);

                // Remove the package from the global NuGet cache
                TestPathUtils.RemovePackageFromGlobalNugetCache("ScriptPackageWithMainCsx");

                //Change the source to force a recompile, now with the missing package.
                code.Append("return 0;");
                File.WriteAllText(pathToScriptFile, code.ToString());

                result = ScriptTestRunner.Default.Execute($"{pathToScriptFile} -s {pathToScriptPackages}");
                Assert.Contains("Try executing/publishing the script", result.output);
            }
        }
Esempio n. 7
0
 public void ShouldNotCreateDefaultFileForFolderWithExistingScriptFiles()
 {
     using (var scriptFolder = new DisposableFolder())
     {
         ScriptTestRunner.Default.Execute("init custom.csx", scriptFolder.Path);
         ScriptTestRunner.Default.Execute("init", scriptFolder.Path);
         Assert.False(File.Exists(Path.Combine(scriptFolder.Path, "main.csx")));
     }
 }
Esempio n. 8
0
 public void ShouldInitFolderWithCustomFileNAme()
 {
     using (var scriptFolder = new DisposableFolder())
     {
         var result = Execute("init custom.csx", scriptFolder.Path);
         Assert.Equal(0, result.exitCode);
         Assert.True(File.Exists(Path.Combine(scriptFolder.Path, "custom.csx")));
     }
 }
Esempio n. 9
0
 public void ShouldCreateNewScript()
 {
     using (var scriptFolder = new DisposableFolder())
     {
         var result = Execute("new script.csx", scriptFolder.Path);
         Assert.Equal(0, result.exitCode);
         Assert.True(File.Exists(Path.Combine(scriptFolder.Path, "script.csx")));
     }
 }
Esempio n. 10
0
        public void ShouldInitFolderWithCustomFileNameAndExtension()
        {
            using (var scriptFolder = new DisposableFolder())
            {
                var(output, exitCode) = ScriptTestRunner.Default.Execute("init anotherCustom", scriptFolder.Path);

                Assert.Equal(0, exitCode);
                Assert.True(File.Exists(Path.Combine(scriptFolder.Path, "anotherCustom.csx")));
            }
        }
Esempio n. 11
0
 public void ShouldCreateDefaultTargetFrameworkSetting()
 {
     using (var scriptFolder = new DisposableFolder())
     {
         var result = Execute("init", scriptFolder.Path);
         Assert.Equal(0, result.exitCode);
         dynamic settings = JObject.Parse(File.ReadAllText(Path.Combine(scriptFolder.Path, "omnisharp.json")));
         Assert.Equal(RuntimeHelper.TargetFramework, settings.script.defaultTargetFramework.Value);
     }
 }
Esempio n. 12
0
 public void ShouldCreateEnableScriptNugetReferencesSetting()
 {
     using (var scriptFolder = new DisposableFolder())
     {
         var result = Execute("init", scriptFolder.Path);
         Assert.Equal(0, result.exitCode);
         dynamic settings = JObject.Parse(File.ReadAllText(Path.Combine(scriptFolder.Path, "omnisharp.json")));
         Assert.True(settings.script.enableScriptNuGetReferences.Value);
     }
 }
Esempio n. 13
0
        public void ShouldCreateNewScriptWithExtension()
        {
            using (var scriptFolder = new DisposableFolder())
            {
                var(output, exitCode) = ScriptTestRunner.Default.Execute("new anotherScript", scriptFolder.Path);

                Assert.Equal(0, exitCode);
                Assert.True(File.Exists(Path.Combine(scriptFolder.Path, "anotherScript.csx")));
            }
        }
Esempio n. 14
0
 public void ShouldInitializeScriptFolder()
 {
     using (var scriptFolder = new DisposableFolder())
     {
         var result = Execute("init", scriptFolder.Path);
         Assert.Equal(0, result.exitCode);
         Assert.True(File.Exists(Path.Combine(scriptFolder.Path, "main.csx")));
         Assert.True(File.Exists(Path.Combine(scriptFolder.Path, "omnisharp.json")));
         Assert.True(File.Exists(Path.Combine(scriptFolder.Path, ".vscode", "launch.json")));
     }
 }
Esempio n. 15
0
        public void ShouldCreateUnifiedLaunchFileWhenInstalledAsGlobalTool()
        {
            Scaffolder scaffolder = CreateTestScaffolder("somefolder/.dotnet/tools/dotnet-script");

            using (var scriptFolder = new DisposableFolder())
            {
                scaffolder.InitializerFolder("main.csx", scriptFolder.Path);
                var fileContent = File.ReadAllText(Path.Combine(scriptFolder.Path, ".vscode", "launch.json"));
                Assert.Contains("{env:HOME}/.dotnet/tools/dotnet-script", fileContent);
            }
        }
Esempio n. 16
0
        public void ShouldCacheScriptsFromSameFolderIndividually()
        {
            (string Output, bool Cached) Execute(string pathToScript)
            {
                var result = ScriptTestRunner.Default.Execute($"{pathToScript} --debug");

                return(Output : result.output, Cached : result.output.Contains("Using cached compilation"));
            }

            using (var scriptFolder = new DisposableFolder())
            {
                var pathToScriptA = Path.Combine(scriptFolder.Path, "script.csx");
                var pathToScriptB = Path.Combine(scriptFolder.Path, "script");


                var idScriptA = Guid.NewGuid().ToString();
                File.AppendAllText(pathToScriptA, $@"WriteLine(""{idScriptA}"");");

                var idScriptB = Guid.NewGuid().ToString();
                File.AppendAllText(pathToScriptB, $@"WriteLine(""{idScriptB}"");");


                var firstResultOfScriptA = Execute(pathToScriptA);
                Assert.Contains(idScriptA, firstResultOfScriptA.Output);
                Assert.False(firstResultOfScriptA.Cached);

                var firstResultOfScriptB = Execute(pathToScriptB);
                Assert.Contains(idScriptB, firstResultOfScriptB.Output);
                Assert.False(firstResultOfScriptB.Cached);


                var secondResultOfScriptA = Execute(pathToScriptA);
                Assert.Contains(idScriptA, secondResultOfScriptA.Output);
                Assert.True(secondResultOfScriptA.Cached);

                var secondResultOfScriptB = Execute(pathToScriptB);
                Assert.Contains(idScriptB, secondResultOfScriptB.Output);
                Assert.True(secondResultOfScriptB.Cached);


                var idScriptB2 = Guid.NewGuid().ToString();
                File.AppendAllText(pathToScriptB, $@"WriteLine(""{idScriptB2}"");");


                var thirdResultOfScriptA = Execute(pathToScriptA);
                Assert.Contains(idScriptA, thirdResultOfScriptA.Output);
                Assert.True(thirdResultOfScriptA.Cached);

                var thirdResultOfScriptB = Execute(pathToScriptB);
                Assert.Contains(idScriptB, thirdResultOfScriptB.Output);
                Assert.Contains(idScriptB2, thirdResultOfScriptB.Output);
                Assert.False(thirdResultOfScriptB.Cached);
            }
        }
        public void ShouldNotParseLoadDirectiveIgnoringCase()
        {
            using (var rootFolder = new DisposableFolder())
            {
                var rootScript = WriteScript("#LOAD \"Bar.csx\"", rootFolder.Path, "Foo.csx");
                WriteScript(string.Empty, rootFolder.Path, "Bar.csx");
                var scriptFilesResolver = new ScriptFilesResolver();

                var files = scriptFilesResolver.GetScriptFiles(rootScript);

                Assert.Equal(new[] { "Foo.csx" }, files.Select(Path.GetFileName));
            }
        }
Esempio n. 18
0
        public void ShouldRegisterToRunCsxScriptDirectly()
        {
            using (var scriptFolder = new DisposableFolder())
            {
                var(output, exitCode) = ScriptTestRunner.Default.Execute("init", scriptFolder.Path);
                Assert.True(exitCode == 0, output);

                var scriptPath = Path.Combine(scriptFolder.Path, "main.csx");
                var text       = File.ReadAllText(scriptPath);
                Assert.True(text.StartsWith("#!/usr/bin/env dotnet-script"), "should have shebang");
                Assert.True(text.IndexOf("\r\n") < 0, "should have not have windows cr/lf");
            }
        }
Esempio n. 19
0
        public void ShouldThrowMeaningfulExceptionWhenRuntimeTargetIsMissing()
        {
            using (var projectFolder = new DisposableFolder())
            {
                ProcessHelper.RunAndCaptureOutput("dotnet", "new console -n SampleLibrary", projectFolder.Path);
                ProcessHelper.RunAndCaptureOutput("dotnet", "restore", projectFolder.Path);
                var pathToAssetsFile   = Path.Combine(projectFolder.Path, "SampleLibrary", "obj", "project.assets.json");
                var dependencyResolver = new ScriptDependencyContextReader(TestOutputHelper.CreateTestLogFactory());

                var exception = Assert.Throws <InvalidOperationException>(() => dependencyResolver.ReadDependencyContext(pathToAssetsFile));
                Assert.Contains("Make sure that the project file was restored using a RID (runtime identifier).", exception.Message);
            }
        }
Esempio n. 20
0
        public void ShouldCopyDllAndPdbToExecutionCacheFolder()
        {
            using (var scriptFolder = new DisposableFolder())
            {
                var pathToScript = Path.Combine(scriptFolder.Path, "main.csx");

                WriteScript(pathToScript, "#r \"nuget:LightInject, 5.2.1\"", "WriteLine(42);");
                ScriptTestRunner.Default.Execute($"{pathToScript} --nocache");
                var pathToExecutionCache = GetPathToExecutionCache(pathToScript);
                Assert.True(File.Exists(Path.Combine(pathToExecutionCache, "LightInject.dll")));
                Assert.True(File.Exists(Path.Combine(pathToExecutionCache, "LightInject.pdb")));
            }
        }
Esempio n. 21
0
        public void ShouldNotCreateHashWhenScriptIsNotCacheable()
        {
            using (var scriptFolder = new DisposableFolder())
            {
                var pathToScript = Path.Combine(scriptFolder.Path, "main.csx");

                WriteScript(pathToScript, "#r \"nuget:AutoMapper, *\"", "WriteLine(42);");

                var result = Execute(pathToScript);
                Assert.Contains("42", result.output);

                Assert.Null(result.hash);
            }
        }
Esempio n. 22
0
        public void ShouldHandleLocalNuGetConfigWithRelativePath()
        {
            TestPathUtils.RemovePackageFromGlobalNugetCache("NuGetConfigTestLibrary");

            using (var packageLibraryFolder = new DisposableFolder())
            {
                CreateTestPackage(packageLibraryFolder.Path);

                string pathToScriptFile = CreateTestScript(packageLibraryFolder.Path);

                var result = ScriptTestRunner.Default.Execute(pathToScriptFile);
                Assert.Contains("Success", result.output);
            }
        }
        public void ShouldParseFilesStartingWithNuGet()
        {
            using (var rootFolder = new DisposableFolder())
            {
                var rootScript = WriteScript("#load \"NuGet.csx\"", rootFolder.Path, "Foo.csx");
                WriteScript(string.Empty, rootFolder.Path, "NuGet.csx");
                var scriptFilesResolver = new ScriptFilesResolver();

                var files = scriptFilesResolver.GetScriptFiles(rootScript);

                Assert.True(files.Count == 2);
                Assert.Contains(files, f => f.Contains("Foo.csx"));
                Assert.Contains(files, f => f.Contains("NuGet.csx"));
            }
        }
Esempio n. 24
0
        public void ShouldOnlyResolveRootScript()
        {
            using (var rootFolder = new DisposableFolder())
            {
                var rootScript = WriteScript(string.Empty, rootFolder.Path, "Foo.csx");
                WriteScript(string.Empty, rootFolder.Path, "Bar.csx");
                var scriptFilesResolver = new ScriptFilesResolver();

                var files = scriptFilesResolver.GetScriptFiles(rootScript);

                Assert.True(files.Count == 1);
                Assert.Contains(files, f => f.Contains("Foo.csx"));
                Assert.Contains(files, f => !f.Contains("Bar.csx"));
            }
        }
Esempio n. 25
0
        public void ShouldHandleReferencingAssemblyFromScriptFolder()
        {
            using (var workspaceFolder = new DisposableFolder())
            {
                ProcessHelper.RunAndCaptureOutput($"dotnet", $" new classlib -n MyCustomLibrary -o {workspaceFolder.Path}");
                ProcessHelper.RunAndCaptureOutput($"dotnet", $" build -o {workspaceFolder.Path}", workspaceFolder.Path);
                var code     = $@"#r ""MyCustomLibrary.dll"" {Environment.NewLine} WriteLine(""hello world"");";
                var mainPath = Path.Combine(workspaceFolder.Path, "main.csx");
                File.WriteAllText(mainPath, code);

                var publishResult = ScriptTestRunner.Default.Execute("publish main.csx --dll --output .", workspaceFolder.Path);

                Assert.Equal(0, publishResult.exitCode);
            }
        }
Esempio n. 26
0
        public void SimplePublishFromCurrentDirectoryTest()
        {
            using (var workspaceFolder = new DisposableFolder())
            {
                var code     = @"WriteLine(""hello world"");";
                var mainPath = Path.Combine(workspaceFolder.Path, "main.csx");
                File.WriteAllText(mainPath, code);
                var publishResult = ScriptTestRunner.Default.Execute("publish main.csx", workspaceFolder.Path);
                Assert.Equal(0, publishResult.exitCode);

                var exePath             = Path.Combine(workspaceFolder.Path, "publish", _scriptEnvironment.RuntimeIdentifier, "script");
                var executableRunResult = _commandRunner.Execute(exePath);

                Assert.Equal(0, executableRunResult);
            }
        }
Esempio n. 27
0
        public void SimplePublishDllTest()
        {
            using (var workspaceFolder = new DisposableFolder())
            {
                var code     = @"WriteLine(""hello world"");";
                var mainPath = Path.Combine(workspaceFolder.Path, "main.csx");
                File.WriteAllText(mainPath, code);
                var publishResult = ScriptTestRunner.Default.Execute($"publish {mainPath} --dll", workspaceFolder.Path);
                Assert.Equal(0, publishResult.exitCode);

                var dllPath      = Path.Combine("publish", "main.dll");
                var dllRunResult = ScriptTestRunner.Default.Execute($"exec {dllPath}", workspaceFolder.Path);

                Assert.Equal(0, dllRunResult.exitCode);
            }
        }
Esempio n. 28
0
        public void ShouldParseProjectFile()
        {
            using (var projectFolder = new DisposableFolder())
            {
                var projectFile       = new ProjectFile();
                var pathToProjectFile = Path.Combine(projectFolder.Path, "project.csproj");
                projectFile.PackageReferences.Add(new PackageReference("SomePackage", "1.2.3"));
                projectFile.PackageReferences.Add(new PackageReference("AnotherPackage", "3.2.1"));
                projectFile.Save(Path.Combine(projectFolder.Path, "project.csproj"));

                var parsedProjectFile = new ProjectFile(File.ReadAllText(pathToProjectFile));

                Assert.Contains(new PackageReference("SomePackage", "1.2.3"), parsedProjectFile.PackageReferences);
                Assert.Contains(new PackageReference("AnotherPackage", "3.2.1"), parsedProjectFile.PackageReferences);
            }
        }
Esempio n. 29
0
        public void ShouldInitializeScriptFolderContainingWhitespace()
        {
            using (var scriptFolder = new DisposableFolder())
            {
                var path = Path.Combine(scriptFolder.Path, "Folder with whitespace");
                Directory.CreateDirectory(path);

                var(output, exitCode) = ScriptTestRunner.Default.Execute("init", path);

                Assert.Equal(0, exitCode);
                Assert.DoesNotContain("No such file or directory", output, StringComparison.OrdinalIgnoreCase);
                Assert.True(File.Exists(Path.Combine(path, "main.csx")));
                Assert.True(File.Exists(Path.Combine(path, "omnisharp.json")));
                Assert.True(File.Exists(Path.Combine(path, ".vscode", "launch.json")));
            }
        }
        public void SimplePublishTest()
        {
            using (var workspaceFolder = new DisposableFolder())
            {
                var code     = @"WriteLine(""hello world"");";
                var mainPath = Path.Combine(workspaceFolder.Path, "main.csx");
                File.WriteAllText(mainPath, code);
                var args          = new string[] { "publish", mainPath };
                var publishResult = Execute(string.Join(" ", args), workspaceFolder.Path);
                Assert.Equal(0, publishResult.exitCode);

                var exePath             = Path.Combine(workspaceFolder.Path, "publish", _scriptEnvironment.RuntimeIdentifier, "script");
                var executableRunResult = _commandRunner.Execute(exePath);

                Assert.Equal(0, executableRunResult);
            }
        }