0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-31 09:14:20 -04:00
denoland-deno/cli/tests/integration/init_tests.rs
Leo Kettmeir 1ffbd56164
feat: add "deno init" subcommand (#15469)
This adds an init subcommand to that creates a project starter similar to cargo init.

```
$ deno init my_project
Project initialized
Run these commands to get started:
  cd my_project
  deno run main.ts
  deno run main_test.ts
$ deno run main.ts
Add 2 + 3 5
$ cat main.ts
export function add(a: number, b: number): number {
  return a + b;
}
if (import.meta.main) {
  console.log("Add 2 + 3", add(2, 3));
}
$ cat main_test.ts
import { assertEquals } from "https://deno.land/std@0.151.0/testing/asserts.ts";
import { add } from "./main.ts";
Deno.test(function addTest() {
    assertEquals(add(2, 3), 5);
});
```

Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
2022-08-20 01:37:05 +02:00

110 lines
2.9 KiB
Rust

// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
use std::process::Stdio;
use test_util as util;
use test_util::TempDir;
use util::assert_contains;
#[test]
fn init_subcommand_without_dir() {
let temp_dir = TempDir::new();
let cwd = temp_dir.path();
let deno_dir = util::new_deno_dir();
let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir);
let output = deno_cmd
.current_dir(cwd)
.arg("init")
.stdout(Stdio::piped())
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
assert!(output.status.success());
let stdout = String::from_utf8(output.stdout).unwrap();
assert_contains!(stdout, "Project initialized");
assert!(!stdout.contains("cd"));
assert_contains!(stdout, "deno run main.ts");
assert_contains!(stdout, "deno test");
let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir);
let output = deno_cmd
.current_dir(cwd)
.env("NO_COLOR", "1")
.arg("run")
.arg("main.ts")
.stdout(Stdio::piped())
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
assert!(output.status.success());
assert_eq!(output.stdout, b"Add 2 + 3 = 5\n");
let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir);
let output = deno_cmd
.current_dir(cwd)
.env("NO_COLOR", "1")
.arg("test")
.stdout(Stdio::piped())
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
assert!(output.status.success());
let stdout = String::from_utf8(output.stdout).unwrap();
assert_contains!(stdout, "1 passed");
}
#[test]
fn init_subcommand_with_dir_arg() {
let temp_dir = TempDir::new();
let cwd = temp_dir.path();
let deno_dir = util::new_deno_dir();
let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir);
let output = deno_cmd
.current_dir(cwd)
.arg("init")
.arg("my_dir")
.stdout(Stdio::piped())
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
assert!(output.status.success());
let stdout = String::from_utf8(output.stdout).unwrap();
assert_contains!(stdout, "Project initialized");
assert_contains!(stdout, "cd my_dir");
assert_contains!(stdout, "deno run main.ts");
assert_contains!(stdout, "deno test");
let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir);
let output = deno_cmd
.current_dir(cwd)
.env("NO_COLOR", "1")
.arg("run")
.arg("my_dir/main.ts")
.stdout(Stdio::piped())
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
assert!(output.status.success());
assert_eq!(output.stdout, b"Add 2 + 3 = 5\n");
let mut deno_cmd = util::deno_cmd_with_deno_dir(&deno_dir);
let output = deno_cmd
.current_dir(cwd)
.env("NO_COLOR", "1")
.arg("test")
.arg("my_dir/main_test.ts")
.stdout(Stdio::piped())
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
assert!(output.status.success());
let stdout = String::from_utf8(output.stdout).unwrap();
assert_contains!(stdout, "1 passed");
}