|
| 1 | +"Test nodejs command line" |
| 2 | + |
| 3 | +import os, sys, subprocess |
| 4 | + |
| 5 | + |
| 6 | +THIS_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 7 | + |
| 8 | + |
| 9 | +def test_runs(): |
| 10 | + assert subprocess.call([sys.executable, "-m", "nodejs", "--version"]) == 0 |
| 11 | + |
| 12 | + |
| 13 | +def test_version(capfd): |
| 14 | + subprocess.call([sys.executable, "-m", "nodejs", "--version"]) |
| 15 | + out, err = capfd.readouterr() |
| 16 | + assert out.startswith('v') |
| 17 | + |
| 18 | + |
| 19 | +def test_eval(capfd): |
| 20 | + subprocess.call([sys.executable, "-m", "nodejs", "--eval", "console.log('hello')"]) |
| 21 | + out, err = capfd.readouterr() |
| 22 | + assert out.strip() == 'hello' |
| 23 | + |
| 24 | + |
| 25 | +def test_eval_error(capfd): |
| 26 | + subprocess.call([sys.executable, "-m", "nodejs", "--eval", "console.error('error')"]) |
| 27 | + out, err = capfd.readouterr() |
| 28 | + assert err.strip() == 'error' |
| 29 | + |
| 30 | + |
| 31 | +def test_eval_error_exit(): |
| 32 | + ret = subprocess.call([sys.executable, "-m", "nodejs", "--eval", "process.exit(1)"]) |
| 33 | + assert ret == 1 |
| 34 | + |
| 35 | + |
| 36 | +def test_script(capfd): |
| 37 | + subprocess.call([sys.executable, "-m", "nodejs", os.path.join(THIS_DIR, "test_node", "test_script.js")]) |
| 38 | + out, err = capfd.readouterr() |
| 39 | + assert out.strip() == 'hello' |
| 40 | + |
| 41 | + |
| 42 | +def test_args(capfd): |
| 43 | + subprocess.call([sys.executable, "-m", "nodejs", os.path.join(THIS_DIR, "test_node", "test_args.js"), "hello"]) |
| 44 | + out, err = capfd.readouterr() |
| 45 | + assert out.strip() == 'hello' |
| 46 | + |
| 47 | + |
| 48 | +def test_npm_runs(): |
| 49 | + assert subprocess.call([sys.executable, "-m", "nodejs.npm", "--version"]) == 0 |
| 50 | + |
| 51 | + |
| 52 | +def test_npm_version(capfd): |
| 53 | + subprocess.call([sys.executable, "-m", "nodejs.npm", "--version"]) |
| 54 | + out, err = capfd.readouterr() |
| 55 | + assert isinstance(out, str) |
| 56 | + |
| 57 | + |
| 58 | +def test_install_package(tmp_path, capfd): |
| 59 | + os.chdir(tmp_path) |
| 60 | + subprocess.call([sys.executable, "-m", "nodejs.npm", "init", "-y"]) |
| 61 | + assert (tmp_path / 'package.json').exists() |
| 62 | + subprocess.call([sys.executable, "-m", "nodejs.npm", "install", "is-even"]) |
| 63 | + assert (tmp_path / 'node_modules' / 'is-even').exists() |
| 64 | + out, err = capfd.readouterr() |
| 65 | + subprocess.call([sys.executable, "-m", "nodejs", "--eval", 'console.log(require("is-even")(42))']) |
| 66 | + out, err = capfd.readouterr() |
| 67 | + assert out.strip() == 'true' |
| 68 | + subprocess.call([sys.executable, "-m", "nodejs", "--eval", 'console.log(require("is-even")(43))']) |
| 69 | + out, err = capfd.readouterr() |
| 70 | + assert out.strip() == 'false' |
0 commit comments