-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from janpfeifer/script
%%script implementation.
- Loading branch information
Showing
8 changed files
with
410 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "fe105ef6-5264-4cb4-b467-60d9b0a337fd", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"1 : a\n", | ||
"2 : b\n", | ||
"3 : c\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"%%bash\n", | ||
"((ii=1))\n", | ||
"(\n", | ||
" cat <<EOF\n", | ||
"a\n", | ||
"b\n", | ||
"c\n", | ||
"EOF\n", | ||
") | while read str ; do \n", | ||
" echo \"$ii : $str\"\n", | ||
" ((ii+=1))\n", | ||
"done" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "004e9622-e3a4-4780-816a-e5d1c77055b4", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"18\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"%%script bc\n", | ||
"7 + 11" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "a90b5943-54f0-4b12-b1b5-35d085a29bb2", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"19\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"%%sh\n", | ||
"X=19\n", | ||
"echo $X" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "fdf96ecb-8342-479d-822b-ff9707977d9d", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"ename": "ERROR", | ||
"evalue": "executing special commands in cell: \"%%sh\" can only appear at the start of the cell", | ||
"output_type": "error", | ||
"traceback": [ | ||
"executing special commands in cell: \"%%sh\" can only appear at the start of the cell" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"X=17\n", | ||
"%%sh\n", | ||
"echo $X" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Go (gonb)", | ||
"language": "go", | ||
"name": "gonb" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": "", | ||
"file_extension": ".go", | ||
"mimetype": "", | ||
"name": "go", | ||
"nbconvert_exporter": "", | ||
"pygments_lexer": "", | ||
"version": "go1.22.1" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package nbtests | ||
|
||
import ( | ||
"fmt" | ||
"github.com/stretchr/testify/require" | ||
"k8s.io/klog/v2" | ||
"os" | ||
"path" | ||
"testing" | ||
) | ||
|
||
// TestWritefile tests that `%%writefile` tests that `%%writefile` works | ||
func TestWritefile(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("Skipping integration (nbconvert) test for short tests.") | ||
return | ||
} | ||
klog.Infof("GOCOVERDIR=%s", os.Getenv("GOCOVERDIR")) | ||
|
||
// Create directory where to write the file, and set TEST_DIR env variable. | ||
testDir, err := os.MkdirTemp("", "gonb_nbtests_writefile_") | ||
require.NoError(t, err) | ||
require.NoError(t, os.Setenv("TEST_DIR", testDir+"/")) | ||
klog.Infof("TEST_DIR=%s/", testDir) | ||
|
||
// Run notebook test. | ||
notebook := "writefile" | ||
f := executeNotebook(t, notebook) | ||
err = Check(f, | ||
Sequence( | ||
Match( | ||
OutputLine(1), | ||
Separator, | ||
fmt.Sprintf(`Cell contents written to "%s/poetry.txt".`, testDir), | ||
Separator, | ||
), | ||
Match( | ||
OutputLine(2), | ||
Separator, | ||
fmt.Sprintf(`Cell contents appended to "%s/poetry.txt".`, testDir), | ||
Separator, | ||
), | ||
), *flagPrintNotebook) | ||
|
||
require.NoError(t, err) | ||
require.NoError(t, f.Close()) | ||
require.NoError(t, os.Remove(f.Name())) | ||
clearNotebook(t, notebook) | ||
|
||
// Checks the file was written. | ||
filePath := path.Join(testDir, "poetry.txt") | ||
contents, err := os.ReadFile(filePath) | ||
require.NoErrorf(t, err, "Failed to read file %q that should have been written by the notebook.", filePath) | ||
want := `Um trem-de-ferro é uma coisa mecânica, | ||
mas atravessa a noite, a madrugada, o dia, | ||
atravessou minha vida, | ||
virou só sentimento. | ||
Adélia Prado | ||
` | ||
require.Equalf(t, want, string(contents), "Contents written to %q don't match", filePath) | ||
|
||
// Remove directory. | ||
require.NoError(t, os.RemoveAll(testDir)) | ||
} | ||
|
||
// TestScript tests the cell magic `%%script` (and `%%bash` and `%%sh`). | ||
// It requires `bc` to be installed -- I assume available in most unixes. | ||
func TestScript(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("Skipping integration (nbconvert) test for short tests.") | ||
return | ||
} | ||
klog.Infof("GOCOVERDIR=%s", os.Getenv("GOCOVERDIR")) | ||
|
||
// Run notebook test. | ||
notebook := "script" | ||
f := executeNotebook(t, notebook) | ||
err := Check(f, | ||
Sequence( | ||
Match( | ||
OutputLine(1), | ||
Separator, | ||
"1 : a", | ||
"2 : b", | ||
"3 : c", | ||
Separator, | ||
), | ||
Match( | ||
OutputLine(2), | ||
Separator, | ||
"18", | ||
Separator, | ||
), | ||
Match( | ||
OutputLine(3), | ||
Separator, | ||
"19", | ||
Separator, | ||
), | ||
Match( | ||
OutputLine(4), | ||
Separator, | ||
"", | ||
"can only appear at the start", // ... can only appear ... | ||
"", | ||
Separator, | ||
), | ||
), *flagPrintNotebook) | ||
|
||
require.NoError(t, err) | ||
require.NoError(t, f.Close()) | ||
require.NoError(t, os.Remove(f.Name())) | ||
clearNotebook(t, notebook) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.