def test_init(): p = parser.Parser(['chrome.init id secret ref']) p.execute() assert p.variables['client_id'] == 'id' assert p.variables['client_secret'] == 'secret' assert p.variables['refresh_token'] == 'ref'
def test_multiple_commands(): """ Advanced scenario. Set environment, assign environment to local vars, use those to call several functions. """ cli = 'cli' sec = 'sec' ref = 'ref' app = 'app' os.environ['clientid'] = cli os.environ['secret'] = sec os.environ['reftoken'] = ref script = [ 'id = ${env.clientid}', 'secret = ${env.secret}', 'ref = ${env.reftoken}', 'chrome.init ${id} ${secret} ${ref}', 'chrome.setapp {}'.format(app), ] p = parser.Parser(script=script) foo_obj = flexmock() foo_obj.should_receive('chromeinit').with_args(p, cli, sec, ref).once() foo_obj.should_receive('chromesetapp').with_args(p, app).once() p.functions['chrome.init'] = foo_obj.chromeinit p.functions['chrome.setapp'] = foo_obj.chromesetapp p.execute()
def test_publish_incorrect_tgt(): p = parser.Parser('foo') p.execute_line('chrome.init id secret ref') with pytest.raises(ValueError): p.execute_line('chrome.publish abc')
def test_setapp(): p = parser.Parser(['chrome.init id secret ref']) p.execute() with pytest.raises(KeyError): assert not p.variables['app_id'] p.execute_line('chrome.setapp appid') assert p.variables['app_id'] == 'appid' assert p.variables['chrome_store'].app_id == 'appid'
def test_cd(): p = parser.Parser("foo") startdir = os.getcwd() p.execute_line("cd tests") assert os.getcwd() == os.path.join(startdir, 'tests') os.chdir(startdir) assert os.getcwd() == startdir
def test_popd_empty(): p = parser.Parser("foo") startdir = os.getcwd() p.execute_line("pushd tests") assert os.getcwd() == os.path.join(startdir, 'tests') p.execute_line("popd") with pytest.raises(IndexError): p.execute_line("popd")
def test_upload(): """ Test if chrome store's upload/update function is called as expected (after its initialization). """ p = parser.Parser('foo') p.execute_line('chrome.init id secret ref') mock_store = flexmock(p.variables['chrome_store'] ) # Mock the store, do not actually send anything mock_store.should_receive('upload').with_args('fn', False).once() p.variables['chrome_store'] = mock_store p.execute_line('chrome.update fn')
def test_publish(txt, target): """ Test if chrome store's publish function is called as expected. """ p = parser.Parser('foo') p.execute_line('chrome.init id secret ref') mock_store = flexmock(p.variables['chrome_store'] ) # Mock the store, do not actually send anything mock_store.should_receive('publish').with_args(target).once() p.variables['chrome_store'] = mock_store p.execute_line('chrome.publish {}'.format(txt))
def test_check_version(): p = parser.Parser('foo') p.execute_line('chrome.init id secret ref') p.execute_line('chrome.setapp appid') mock_store = flexmock(p.variables['chrome_store'] ) # Mock the store, do not actually send anything mock_store.should_receive('get_uploaded_version').with_args().and_return( '1.0.12345').once() p.variables['chrome_store'] = mock_store p.execute_line('chrome.check_version 1.0.12345')
def test_unpack(): zip_fn = 'tests/files/sample_zip.zip' target_dir = 'tests/files/tempfolder' p = parser.Parser('foo') p.execute_line('chrome.unpack {} {}'.format(zip_fn, target_dir)) assert os.path.exists(zip_fn) with open(os.path.join(target_dir, 'hello')) as f: assert f.read().find( "Sample content of zip" ) != -1 # Make sure expected content is present in archive shutil.rmtree(target_dir)
def test_pushd_popd(): p = parser.Parser("foo") startdir = os.getcwd() p.execute_line("pushd tests") assert os.getcwd() == os.path.join(startdir, 'tests') p.execute_line("pushd files") assert os.getcwd() == os.path.join(startdir, 'tests', 'files') p.execute_line("popd") assert os.getcwd() == os.path.join(startdir, 'tests') p.execute_line("popd") assert os.getcwd() == startdir
def test_environ_init(): """ Set up variables in environment and check parser uses them to init properly. """ os.environ['client_id'] = 'x' os.environ['client_secret'] = 'y' os.environ['refresh_token'] = 'z' p = parser.Parser([ 'chrome.init ${env.client_id} ${env.client_secret} ${env.refresh_token}' ]) p.execute() assert p.variables['client_id'] == 'x' assert p.variables['client_secret'] == 'y' assert p.variables['refresh_token'] == 'z'
def test_call_function(): """ Test that function name is parsed and called correctly. """ p = parser.Parser(['foo.func a b c', 'foo.func2 x y', 'foo.func2 x y']) foo_obj = flexmock() foo_obj.should_receive('foo_func').with_args(p, 'a', 'b', 'c').once() foo_obj.should_receive('foo_func2').with_args(p, 'x', 'y').twice() foo_obj.should_receive('foo_funca').never() # check that no other function is called p.functions['foo.func'] = foo_obj.foo_func p.functions['foo.func2'] = foo_obj.foo_func2 p.execute()
def test_zip(): zip_fn = os.path.join(os.getcwd(), 'testzip.zip') shutil.rmtree(zip_fn, ignore_errors=True) if os.path.exists(zip_fn): os.remove(zip_fn) assert not os.path.exists(zip_fn) p = parser.Parser("foo") p.execute_line("zip tests/files/sample_folder testzip.zip") assert os.path.exists(zip_fn) archive = zipfile.ZipFile(zip_fn, 'r') txt = archive.read('hello').decode("utf-8") assert txt.find("Sample bare content" ) != -1 # Make sure expected content is present in archive archive.close() os.remove(zip_fn)
def test_resolve_variable_unset(): p = parser.Parser(['foo']) with pytest.raises(parser.VariableNotDefinedException): p.resolve_variable("${xyz}") == 'hello'
def test_resolve_env_variable_unset(): os.environ['xyz'] = '' p = parser.Parser(['foo']) assert not p.resolve_variable("${env.xyz}")
def test_resolve_variable(): p = parser.Parser(['foo']) p.variables['abc'] = 'hello' assert p.resolve_variable("${abc}") == 'hello'
def test_new_no_init(): p = parser.Parser(['chrome.new fn']) with pytest.raises(parser.InvalidStateException): p.execute()
def test_assignment_syntax_err(command, var, value): p = parser.Parser([command]) with pytest.raises(parser.FunctionNotDefinedException): p.execute()
def test_assignment(command, var, value): p = parser.Parser([command]) p.execute() assert p.variables[var] == value
def test_publish_no_init(): p = parser.Parser(['chrome.publish trusted']) with pytest.raises(parser.InvalidStateException): p.execute()
def test_upload_no_init(): p = parser.Parser(['chrome.update fn']) with pytest.raises(parser.InvalidStateException): p.execute()
def test_resolve_env_variable(): os.environ['abc'] = 'hello' p = parser.Parser(['foo']) assert p.resolve_variable("${env.abc}") == 'hello'