Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dataloaders tests #110

Merged
merged 5 commits into from
Nov 9, 2023
Merged

dataloaders tests #110

merged 5 commits into from
Nov 9, 2023

Conversation

Fil
Copy link
Contributor

@Fil Fil commented Nov 7, 2023

PR edited to salvage the data loaders tests.

Previously:

Use node for .js loaders, tsx for .ts loaders

addresses #96

Note: as expected, this breaks existing loaders ending in .js or .ts and that expect to be called as (non-javascript) shell scripts. However node ignores the #! line so if those loaders were already written in js or ts they should keep running.

(The test for node is not great, in the sense that if the data1.js loader is called with tsx it works the same; I don't know how to distinguish these cases since in the end tsx is just a node script.)

This PR does not cover the executable mode issue.

test/dataloaders-test.ts Outdated Show resolved Hide resolved
test/dataloaders-test.ts Outdated Show resolved Hide resolved
function makeCommand(commandPath) {
if (commandPath.endsWith(".js")) return {command: "node", options: [commandPath]};
if (commandPath.endsWith(".ts")) return {command: "tsx", options: [commandPath]};
return {command: commandPath, options: []};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should automatically assuming non .js/.ts files are executable or add a check for the .sh extension? We also haven't checked for compatibility with Windows and it should be something to consider before launch

not a blocker for this PR but something to think about

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently the data loaders are ignored all together if non executable. But I want to separate that issue (see #96 (comment)); the call failing because the script is not +x would just be one of the many possible errors anyway.

@@ -0,0 +1,6 @@
function mytest() {
// TODO: how to make sure this is not called by tsx? (and, does it matter?)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you wanted to test this, you could test the error case: if you put TypeScript annotations in a .js file, you should get a syntax error. It’d probably be a good thing to test how runLoader handles errors anyway!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that but couldn't make it work, because tsx sends the syntax error anyway since the file extension is js.

(Note that STDERR is passed to the parent (for logging), with the "inherit" option.)

test:

 it("a .js data loader is _not_ called with tsx", async () => {
    const outputPath = ".observablehq/data4.txt";
    await runLoader("test/dataloaders/data4-error.txt.js", outputPath);
    try {
      assert.strictEqual(await readFile(outputPath, "utf-8"), "");
    } catch (error) {
      assert.strictEqual(error.code, "ENOENT");
    } finally {
      unlink(outputPath);
    }
});

test/dataloaders/data4-error.txt.js

/* eslint-disable */
function mytest2(): string {
  return "tsx";
}

console.log(mytest2());

@mbostock mbostock mentioned this pull request Nov 8, 2023
@Fil Fil marked this pull request as draft November 8, 2023 19:05
@Fil
Copy link
Contributor Author

Fil commented Nov 8, 2023

I'll want to add the new tests to #114 at some point.

@Fil Fil changed the base branch from main to fil/fix-130 November 9, 2023 12:45
@Fil Fil changed the title run dataloaders with the appropriate command dataloaders tests Nov 9, 2023
@Fil
Copy link
Contributor Author

Fil commented Nov 9, 2023

I've edited this PR to just add tests for data loaders

@Fil Fil marked this pull request as ready for review November 9, 2023 12:48
Base automatically changed from fil/fix-130 to main November 9, 2023 13:32
.gitignore Outdated
@@ -3,4 +3,5 @@ dist/
docs/.observablehq/cache
node_modules/
test/output/*-changed.*
test/.observablehq/cache
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn’t be generating anything here now that #120 is merged. The data loaders were using the wrong cache directory. (We were erroneously using .observablehq/cache relative to the current working directory, rather than the intended source root.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even after merging #120 these tests are saved in test/.observablehq/cache/ (and I think that should be the intended source root for the tests—we don't want to interfere with docs?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, let me double-check what’s happening here. It may indeed be the case that the source root for the tests is the test folder.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, indeed you are passing in "test" as the first argument to Loader.load, so that makes sense. 👍

test/dataloaders-test.ts Outdated Show resolved Hide resolved
@mbostock mbostock enabled auto-merge (squash) November 9, 2023 15:47
@mbostock mbostock merged commit dc0cac1 into main Nov 9, 2023
1 check passed
@mbostock mbostock deleted the fil/node-tsx branch November 9, 2023 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants