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

testing: add runIfMain #152

Closed
ry opened this issue Jan 24, 2019 · 7 comments
Closed

testing: add runIfMain #152

ry opened this issue Jan 24, 2019 · 7 comments
Labels

Comments

@ry
Copy link
Member

ry commented Jan 24, 2019

Once we support imports.meta.url and have a way for testing if the current script is the main script, we want to have a two line snippit of code that will allow any test to be executed as a standalone test deno foo_test.ts rather than running the main entry point.
Essentially something like this:
https://github.com/tensorflow/tensorflow/blob/c4e61ac1fd0b3b48461e2876b20fe56c09faa2ff/tensorflow/python/lib/io/tf_record_test.py#L484

The most basic form would be something like this:

if (args[1] == imports.meta.url) {
  testMain();
}

But perhaps we can make this shorter by having a function like runIfMain(imports.meta.url)

This came up in: #129

@ry
Copy link
Member Author

ry commented Feb 3, 2019

import.meta.url is now available in 0.2.10 so runTestsIfMain should be doable now.

@bartlomieju
Copy link
Member

bartlomieju commented Feb 3, 2019

I guess this belongs to testing/mod.ts but that file contains this notice: // Do not add imports in this file in order to be compatible with Node.. So import { args } from "deno"; isn't desired there. Any thoughts?

EDIT:
On the other hand

if __name__ == "__main__":
  do_something()

is a very commonly used idiom in Python. Maybe we could add runIfMain to deno itself? Then Python example would be written as:

import { runIfMain } from "deno";

runIfMain(doSomething);
// or
runIfMain(() => {
   doSomething1();
   doSomething2();
});

Just thinking out loud.

@bartlomieju
Copy link
Member

@ry any more thoughts on this?

@ry
Copy link
Member Author

ry commented Feb 12, 2019

@bartlomieju Yes - I think if we implement window.location as described in denoland/deno#1750 then we can do

if (imports.meta.url == window.location.toString()) {
  runTests();
}

@ry
Copy link
Member Author

ry commented Feb 19, 2019

FYI window.location has landed in Deno v0.3.0

One can test if the script is main by using this:

if (window.location.toString() == import.meta.url) {
  console.log("main");
}

We should be able to adapt for testing.runIfMain(import.meta)

See the manual entry.

@bartlomieju
Copy link
Member

Closed in #215

@bartlomieju
Copy link
Member

CC @ry

@ry ry closed this as completed May 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants