Skip to content

Commit 36409a6

Browse files
committed
Now detecting if tests are submitted
1 parent c91f5f3 commit 36409a6

File tree

2 files changed

+80
-25
lines changed

2 files changed

+80
-25
lines changed

src/NotebookUnit/RunTestsMagicCommand.cs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Threading.Tasks;
55
using Microsoft.DotNet.Interactive;
66
using Microsoft.DotNet.Interactive.Events;
7+
using Microsoft.DotNet.Interactive.Formatting;
78

89
namespace NotebookUnit
910
{
@@ -14,28 +15,38 @@ public async Task OnLoadAsync(Kernel kernel)
1415

1516
var testCommand = new Command("#!test", "Run the tests in this notebook")
1617
{
17-
new Option<string>("-s", "Dunno why I'm adding this, I just want it to run")
18+
new Option<Action[]>(new string[] {"-t", "--tests" }, () => new Action[] { }, "Specify tests to run")
1819
};
1920

20-
testCommand.Handler = CommandHandler.Create(async (KernelInvocationContext context) =>
21-
{
22-
context.Publish(
23-
new DisplayedValueProduced
24-
("This is a test",
25-
context.Command,
26-
new[] { new FormattedValue("text/plain", "This is my formatted value") }
27-
)
28-
);
29-
});
21+
testCommand.Handler =
22+
CommandHandler.Create(async (Action[] tests, KernelInvocationContext context) =>
23+
RunTests(tests, context));
3024

3125
kernel.AddDirective(testCommand);
3226

33-
// This doesn't appear to be working
34-
//if (KernelInvocationContext.Current is { } context)
35-
//{
36-
// await context.DisplayAsync($"`{nameof(RunTestsMagicCommand)}` is loaded. It adds unit test capabilities");
37-
//}
27+
if (KernelInvocationContext.Current is { } context)
28+
{
29+
await context.DisplayAsync($"`{nameof(RunTestsMagicCommand)}` is loaded. It enables testing in notebooks", "text/markdown");
30+
}
3831

3932
}
40-
}
33+
34+
private object RunTests(Action[] tests, KernelInvocationContext context)
35+
{
36+
37+
if (tests == null || tests.Length == 0) {
38+
context.Fail(message: "No tests");
39+
return null;
40+
}
41+
42+
context.Publish(new DisplayedValueProduced("Tests to be run", context.Command, new[] {
43+
new FormattedValue(PlainTextFormatter.MimeType, "Tests will be run")
44+
}));
45+
46+
return new object();
47+
48+
}
49+
50+
51+
}
4152
}

src/TestnotebookUnit.ipynb

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"text/html": [
1111
"\r\n",
1212
"<div>\r\n",
13-
" <div id='dotnet-interactive-this-cell-28751.6c5bc3419e7d4f0fba9610b517a8d24c' style='display: none'>\r\n",
13+
" <div id='dotnet-interactive-this-cell-21552.91bdddaa9c0f49eeb06e70d46df15153' style='display: none'>\r\n",
1414
" The below script needs to be able to find the current output cell; this is an easy method to get it.\r\n",
1515
" </div>\r\n",
1616
" <script type='text/javascript'>\r\n",
@@ -70,23 +70,24 @@
7070
"}\r\n",
7171
"\r\n",
7272
"function loadDotnetInteractiveApi() {\r\n",
73-
" probeAddresses([\"http://192.168.1.151:1025/\", \"http://127.0.0.1:1025/\"])\r\n",
73+
" probeAddresses([\"http://192.168.1.71:1000/\", \"http://172.19.160.1:1000/\", \"http://127.0.0.1:1000/\"])\r\n",
7474
" .then((root) => {\r\n",
7575
" // use probing to find host url and api resources\r\n",
7676
" // load interactive helpers and language services\r\n",
7777
" let dotnetInteractiveRequire = require.config({\r\n",
78-
" context: '28751.6c5bc3419e7d4f0fba9610b517a8d24c',\r\n",
78+
" context: '21552.91bdddaa9c0f49eeb06e70d46df15153',\r\n",
7979
" paths: {\r\n",
8080
" 'dotnet-interactive': `${root}resources`\r\n",
8181
" }\r\n",
8282
" }) || require;\r\n",
8383
"\r\n",
8484
" let dotnetInteractiveExtensionsRequire = require.config({\r\n",
85-
" context: '28751.6c5bc3419e7d4f0fba9610b517a8d24c',\r\n",
85+
" context: '21552.91bdddaa9c0f49eeb06e70d46df15153',\r\n",
8686
" paths: {\r\n",
8787
" 'dotnet-interactive-extensions': `${root}extensions`\r\n",
8888
" }\r\n",
8989
" }) || require;\r\n",
90+
"\r\n",
9091
" if (!window.dotnetInteractiveRequire) {\r\n",
9192
" window.dotnetInteractiveRequire = dotnetInteractiveRequire;\r\n",
9293
" }\r\n",
@@ -131,7 +132,16 @@
131132
{
132133
"data": {
133134
"text/html": [
134-
"<div><strong>Restore sources</strong><ul><li><span>/home/csharpfritz/dev/csharp_with_csharpfritz/src/NotebookUnit/bin/Debug</span></li></ul></div>"
135+
"<div><strong>Restore sources</strong><ul><li><span>c:/dev/csharp_with_csharpfritz/src/NotebookUnit/bin/Debug</span></li></ul></div>"
136+
]
137+
},
138+
"metadata": {},
139+
"output_type": "display_data"
140+
},
141+
{
142+
"data": {
143+
"text/html": [
144+
"<div><strong>Restore sources</strong><ul><li><span>c:/dev/csharp_with_csharpfritz/src/NotebookUnit/bin/Debug</span></li><li><span>c:/dev/csharp_with_csharpfritz/src</span></li></ul></div>"
135145
]
136146
},
137147
"metadata": {},
@@ -145,20 +155,54 @@
145155
},
146156
"metadata": {},
147157
"output_type": "display_data"
158+
},
159+
{
160+
"data": {
161+
"text/plain": [
162+
"Loaded kernel extension \"RunTestsMagicCommand\" from assembly C:\\Users\\jefritz\\.nuget\\packages\\notebookunit\\0.1.0\\interactive-extensions\\dotnet\\NotebookUnit.dll"
163+
]
164+
},
165+
"metadata": {},
166+
"output_type": "display_data"
167+
},
168+
{
169+
"data": {
170+
"text/markdown": [
171+
"`RunTestsMagicCommand` is loaded. It enables testing in notebooks"
172+
]
173+
},
174+
"metadata": {},
175+
"output_type": "display_data"
148176
}
149177
],
150178
"source": [
151-
"#i nuget:/home/csharpfritz/dev/csharp_with_csharpfritz/src/NotebookUnit/bin/Debug\n",
179+
"#i nuget:c:/dev/csharp_with_csharpfritz/src/NotebookUnit/bin/Debug\n",
180+
"#i nuget:c:/dev/csharp_with_csharpfritz/src\n",
181+
"\n",
152182
"#r nuget:NotebookUnit,0.1.0 "
153183
]
154184
},
155185
{
156186
"cell_type": "code",
157187
"execution_count": 2,
158188
"metadata": {},
159-
"outputs": [],
189+
"outputs": [
190+
{
191+
"ename": "Unhandled exception",
192+
"evalue": "No tests",
193+
"output_type": "error",
194+
"traceback": [
195+
"No tests"
196+
]
197+
}
198+
],
160199
"source": [
161-
"#!test -h"
200+
"public void DoThis() {\n",
201+
" display(\"Ran this\");\n",
202+
"}\n",
203+
"\n",
204+
"\n",
205+
"#!test -t new Action[] {() => true}"
162206
]
163207
}
164208
],

0 commit comments

Comments
 (0)