这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DotNetTry.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=trydotnet/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
<s:Boolean x:Key="/Default/UserDictionary/Words/=trydotnet/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Untokenize/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
35 changes: 35 additions & 0 deletions MLS.Agent.Tests/CommandLine/VerifyCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,41 @@ await VerifyCommand.Do(
$"{root}{Path.DirectorySeparatorChar}doc.md*Line 2:*{root}{Path.DirectorySeparatorChar}Program.cs (in project {root}{Path.DirectorySeparatorChar}some.csproj)*".EnforceLF());
}

[Fact]
public async Task Fails_if_language_is_not_compatible_with_backing_project()
{
var root = Create.EmptyWorkspace(isRebuildablePackage: true).Directory;

var directoryAccessor = new InMemoryDirectoryAccessor(root, root)
{
("some.csproj", CsprojContents),
("Program.cs", CompilingProgramCs),
("support.fs", "let a = 0"),
("doc.md", @"
```fs --source-file support.fs --project some.csproj
```
")
}.CreateFiles();

var console = new TestConsole();

await VerifyCommand.Do(
new VerifyOptions(root),
console,
() => directoryAccessor,
PackageRegistry.CreateForTryMode(root));

_output.WriteLine(console.Out.ToString());

console.Out
.ToString()
.EnforceLF()
.Trim()
.Should()
.Match(
$"*Build failed as project {root}{Path.DirectorySeparatorChar}some.csproj is not compatible with language fsharp*".EnforceLF());
}

[Fact]
public async Task When_non_editable_code_blocks_do_not_contain_errors_then_validation_succeeds()
{
Expand Down
59 changes: 49 additions & 10 deletions MLS.Agent.Tests/Markdown/CodeBlockAnnotationExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ static void MyProgram(string[] args)
var document =
$@"```{language} --source-file Program.cs
```";
string html = (await pipeline.RenderHtmlAsync(document)).EnforceLF();
var html = (await pipeline.RenderHtmlAsync(document)).EnforceLF();
html.Should().Contain(fileContent.HtmlEncode().ToString());
}

[Fact]
public async Task Does_not_insert_code_when_specified_language_is_not_csharp()
public async Task Does_not_insert_code_when_specified_language_is_not_supported()
{
string expectedValue =
var expectedValue =
@"<pre><code class=""language-js"">console.log(&quot;Hello World&quot;);
</code></pre>
".EnforceLF();
Expand All @@ -90,27 +90,34 @@ public async Task Does_not_insert_code_when_specified_language_is_not_csharp()
html.Should().Contain(expectedValue);
}

[Fact]
public async Task Does_not_insert_code_when_csharp_is_specified_but_no_additional_options()
[Theory]
[InlineData("cs", "language-cs")]
[InlineData("csharp", "language-csharp")]
[InlineData("c#", "language-c#")]
[InlineData("fs", "language-fs")]
[InlineData("fsharp", "language-fsharp")]
[InlineData("f#", "language-f#")]
public async Task Does_not_insert_code_when_supported_language_is_specified_but_no_additional_options(string fenceLanguage, string expectedClass)
{
string expectedValue =
@"<pre><code class=""language-cs"">Console.WriteLine(&quot;Hello World&quot;);
var expectedValue =
$@"<pre><code class=""{expectedClass}"">Console.WriteLine(&quot;Hello World&quot;);
</code></pre>
".EnforceLF();

var testDir = TestAssets.SampleConsole;
var directoryAccessor = new InMemoryDirectoryAccessor(testDir);
var pipeline = new MarkdownPipelineBuilder().UseCodeBlockAnnotations(directoryAccessor, Default.PackageFinder).Build();
var document = @"
```cs
var document = $@"
```{fenceLanguage}
Console.WriteLine(""Hello World"");
```";
var html = (await pipeline.RenderHtmlAsync(document)).EnforceLF();
html.Should().Contain(expectedValue);
}


[Fact]
public async Task Error_messsage_is_displayed_when_the_linked_file_does_not_exist()
public async Task Error_message_is_displayed_when_the_linked_file_does_not_exist()
{
var testDir = TestAssets.SampleConsole;
var directoryAccessor = new InMemoryDirectoryAccessor(testDir)
Expand Down Expand Up @@ -190,6 +197,38 @@ public async Task Sets_the_trydotnet_package_attribute_using_the_passed_project_
output.Value.Should().Be(fullProjectPath.FullName);
}

[Theory]
[InlineData("cs", "Program.cs", "sample.csproj", "csharp")]
[InlineData("c#", "Program.cs", "sample.csproj", "csharp")]
[InlineData("fs", "Program.fs", "sample.fsproj", "fsharp")]
[InlineData("f#", "Program.fs", "sample.fsproj", "fsharp")]
public async Task Sets_the_trydotnet_language_attribute_using_the_fence_command(string fenceLanguage, string fileName, string projectName, string expectedLanguage)
{
var rootDirectory = TestAssets.SampleConsole;
var currentDir = new DirectoryInfo(Path.Combine(rootDirectory.FullName, "docs"));
var directoryAccessor = new InMemoryDirectoryAccessor(currentDir, rootDirectory)
{
($"src/sample/{fileName}", ""),
($"src/sample/{projectName}", "")
};

var pipeline = new MarkdownPipelineBuilder().UseCodeBlockAnnotations(directoryAccessor, Default.PackageFinder).Build();

var package = $"../src/sample/{projectName}";
var document =
$@"```{fenceLanguage} --project {package} --source-file ../src/sample/{fileName}
```";

var html = (await pipeline.RenderHtmlAsync(document)).EnforceLF();

var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);
var trydotnetLanguage = htmlDocument.DocumentNode
.SelectSingleNode("//pre/code").Attributes["data-trydotnet-language"];

trydotnetLanguage.Value.Should().Be(expectedLanguage);
}

[Fact]
public async Task Sets_the_trydotnet_package_attribute_using_the_passed_package_option()
{
Expand Down
39 changes: 38 additions & 1 deletion MLS.Agent/CommandLine/VerifyCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.DotNet.Try.Markdown;
Expand All @@ -13,6 +14,7 @@
using WorkspaceServer;
using WorkspaceServer.Servers.Roslyn;
using Buffer = Microsoft.DotNet.Try.Protocol.Buffer;
using File = Microsoft.DotNet.Try.Protocol.File;

namespace MLS.Agent.CommandLine
{
Expand Down Expand Up @@ -135,6 +137,17 @@ async Task ReportCompileResults(
.Select(b => b.ProjectOrPackageName())
.FirstOrDefault(name => !string.IsNullOrWhiteSpace(name));

var language = session
.Select(b => b.Language())
.FirstOrDefault(name => !string.IsNullOrWhiteSpace(name));

if (!ProjectIsCompatibleWithLanguage( new UriOrFileInfo(projectOrPackageName), language))
{
SetError();

console.Out.WriteLine($" Build failed as project {projectOrPackageName} is not compatible with language {language}");
}

var editableCodeBlocks = session.Where(b => b.Annotations.Editable).ToList();

var buffers = editableCodeBlocks
Expand Down Expand Up @@ -165,6 +178,7 @@ async Task ReportCompileResults(

var workspace = new Workspace(
workspaceType: projectOrPackageName,
language: language,
files: files.ToArray(),
buffers: buffers.ToArray());

Expand All @@ -173,7 +187,7 @@ async Task ReportCompileResults(

var processed = await mergeTransformer.TransformAsync(workspace);
processed = await inliningTransformer.TransformAsync(processed);
processed = new Workspace(usings: processed.Usings, workspaceType: processed.WorkspaceType, files: processed.Files);
processed = new Workspace(usings: processed.Usings, workspaceType: processed.WorkspaceType, language:processed.Language, files: processed.Files);

var result = await workspaceServer.Value.Compile(new WorkspaceRequest(processed));

Expand Down Expand Up @@ -257,5 +271,28 @@ void ReportCodeLinkageResults(
}
}
}

private static bool ProjectIsCompatibleWithLanguage(UriOrFileInfo projectOrPackage, string language)
{
var supported = true;
if (projectOrPackage.IsFile)
{
var extension = projectOrPackage.FileExtension.ToLowerInvariant();
switch (extension)
{
case ".csproj":
supported = StringComparer.OrdinalIgnoreCase.Compare(language, "csharp") == 0;
break;

case ".fsproj":
supported = StringComparer.OrdinalIgnoreCase.Compare(language, "fsharp") == 0;
break;
default:
supported = false;
break;
}
}
return supported;
}
}
}
11 changes: 7 additions & 4 deletions MLS.Agent/MLS.Agent.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@
<Target Name="BuildCss"
Inputs="$(MSBuildThisFileDirectory)..\Microsoft.DotNet.Try.Styles\sass\trydotnet.scss"
Outputs="$(MSBuildThisFileDirectory)wwwroot\css\trydotnet.css"
BeforeTargets="BeforeBuild">
BeforeTargets="BeforeBuild"
Condition="'$(NCrunch)' != '1'">

<PropertyGroup>
<_TryDotNetCssExists Condition="Exists('$(MSBuildThisFileDirectory)wwwroot\css\trydotnet.css')">true</_TryDotNetCssExists>
Expand All @@ -147,7 +148,7 @@
</ItemGroup>
</Target>

<Target Name="GatherInputs">
<Target Name="GatherInputs" Condition="'$(NCrunch)' != '1'">
<PropertyGroup>
<ClientInputDir>$(MSBuildThisFileDirectory)..\Microsoft.DotNet.Try.Client</ClientInputDir>
<ClientOutputDir>$(MSBuildThisFileDirectory)wwwroot/client</ClientOutputDir>
Expand All @@ -167,7 +168,8 @@
Inputs="@(TryDotNetJsInput)"
Outputs="$(TryDotNetJsFile);$(TryDotNetJsMap)"
DependsOnTargets="GatherInputs"
BeforeTargets="BeforeBuild">
BeforeTargets="BeforeBuild"
Condition="'$(NCrunch)' != '1'">
<PropertyGroup>
<_TryDotNetMinJsExists Condition="Exists('$(TryDotNetJsFile)')">true</_TryDotNetMinJsExists>
</PropertyGroup>
Expand All @@ -189,7 +191,8 @@
Inputs="@(ClientInputFiles)"
Outputs="$(ClientOutputFile)"
DependsOnTargets="GatherInputs"
BeforeTargets="BeforeBuild">
BeforeTargets="BeforeBuild"
Condition="'$(NCrunch)' != '1'">

<PropertyGroup>
<_TryDotNetClientExists Condition="Exists('$(ClientOutputFile)')">true</_TryDotNetClientExists>
Expand Down
1 change: 1 addition & 0 deletions MLS.Agent/MLS.Agent.v3.ncrunchproject
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<Value>..\Microsoft.DotNet.Try.Client\**.*</Value>
<Value>..\Microsoft.DotNet.Try.js\**.*</Value>
<Value>..\Microsoft.DotNet.Try.Styles\**.*</Value>
<Value>wwwroot\**.*</Value>
</AdditionalFilesToIncludeForProject>
</Settings>
</ProjectConfiguration>
5 changes: 5 additions & 0 deletions MLS.Agent/Markdown/AnnotatedCodeBlockExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,10 @@ public static string ProjectOrPackageName(this AnnotatedCodeBlock block)
(block.Annotations as LocalCodeBlockAnnotations)?.Project?.FullName ??
block.Annotations?.Package;
}
public static string Language(this AnnotatedCodeBlock block)
{
return
block.Annotations?.NormalizedLanguage;
}
}
}
Loading