forked from deepmodeling/dpgen2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Han Wang
committed
Feb 4, 2022
1 parent
a1430c0
commit 84b3431
Showing
7 changed files
with
78 additions
and
12 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
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,18 @@ | ||
import sys, subprocess | ||
|
||
def run_command( | ||
cmd, | ||
shell = None, | ||
): | ||
pp = subprocess.Popen( | ||
cmd, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
shell=shell, | ||
) | ||
out, err = pp.communicate() | ||
return_code = pp.poll() | ||
out = out.decode(sys.stdin.encoding) | ||
err = err.decode(sys.stdin.encoding) | ||
return return_code, out, err | ||
|
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
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,48 @@ | ||
import context | ||
import numpy as np | ||
import unittest, json, shutil, os | ||
from pathlib import Path | ||
from dpgen2.utils.run_command import run_command | ||
|
||
class TestRunCommand(unittest.TestCase): | ||
def setUp(self): | ||
self.work_path = Path('work_path') | ||
self.work_path.mkdir(exist_ok=True) | ||
(self.work_path/'foo').write_text('foo') | ||
(self.work_path/'bar').write_text('foo') | ||
|
||
def tearDown(self): | ||
if self.work_path.is_dir(): | ||
shutil.rmtree(self.work_path) | ||
|
||
def test_success_shell(self): | ||
os.chdir(self.work_path) | ||
ret, out, err = run_command(['ls | sort'], shell=True) | ||
self.assertEqual(ret, 0) | ||
self.assertEqual(out, 'bar\nfoo\n') | ||
self.assertEqual(err, '') | ||
os.chdir('..') | ||
|
||
def test_success(self): | ||
os.chdir(self.work_path) | ||
ret, out, err = run_command(['ls']) | ||
self.assertEqual(ret, 0) | ||
self.assertEqual(out, 'bar\nfoo\n') | ||
self.assertEqual(err, '') | ||
os.chdir('..') | ||
|
||
def test_success_foo(self): | ||
os.chdir(self.work_path) | ||
ret, out, err = run_command(['ls', 'foo']) | ||
self.assertEqual(ret, 0) | ||
self.assertEqual(out, 'foo\n') | ||
self.assertEqual(err, '') | ||
os.chdir('..') | ||
|
||
def test_failed(self): | ||
os.chdir(self.work_path) | ||
ret, out, err = run_command(['ls', 'tar']) | ||
self.assertEqual(ret, 2) | ||
self.assertEqual(out, '') | ||
self.assertEqual(err, "ls: cannot access 'tar': No such file or directory\n") | ||
os.chdir('..') |