def test_create_report(): config = Namespace(report='/path/to/report') results = [ sessions.Result( session=Namespace(signature='foosig', name='foo', func=object()), status=sessions.Status.SUCCESS, ) ] with mock.patch.object(io, 'open', autospec=True) as open_: with mock.patch.object(json, 'dump', autospec=True) as dump: answer = tasks.create_report(results, config) assert answer is results dump.assert_called_once_with( { 'result': 1, 'sessions': [{ 'name': 'foo', 'signature': 'foosig', 'result': 'success', 'result_code': 1, 'args': {}, }], }, mock.ANY, indent=2) open_.assert_called_once_with('/path/to/report', 'w')
def test_global_config_constructor(): args = Namespace(noxfile='noxfile', envdir='dir', sessions=['1', '2'], keywords='red and blue', list_sessions=False, reuse_existing_virtualenvs=True, stop_on_first_error=False, posargs=['a', 'b', 'c'], report=None) config = nox.main.GlobalConfig(args) assert config.noxfile == 'noxfile' assert config.envdir == os.path.abspath('dir') assert config.sessions == ['1', '2'] assert config.keywords == 'red and blue' assert config.list_sessions is False assert config.reuse_existing_virtualenvs is True assert config.stop_on_first_error is False assert config.posargs == ['a', 'b', 'c'] args.posargs = ['--', 'a', 'b', 'c'] config = nox.main.GlobalConfig(args) assert config.posargs == ['a', 'b', 'c']
def test_discover_session_functions_decorator(): # Define sessions using the decorator. @nox.session def foo(): pass @nox.session def bar(): pass def notasession(): pass # Mock up a nox.py module and configuration. mock_module = Namespace( __name__=foo.__module__, foo=foo, bar=bar, notasession=notasession, ) config = Namespace(sessions=(), keywords=()) # Get the manifest and establish that it looks like what we expect. manifest = tasks.discover_manifest(mock_module, config) sessions = list(manifest) assert [s.func for s in sessions] == [foo, bar] assert [str(i) for i in sessions] == ['foo', 'bar']
def test_discover_manifest_naming_convention(): # Define sessions with the correct naming convention. def session_1(): pass def session_2(): pass def notasession(): pass # Mock up a nox.py module and configuration. mock_module = Namespace( __name__='irrelevant', session_1=session_1, session_2=session_2, notasession=notasession, ) config = Namespace(sessions=(), keywords=()) # Get the manifest and establish that it looks like what we expect. manifest = tasks.discover_manifest(mock_module, config) sessions = list(manifest) assert [s.func for s in sessions] == [session_1, session_2] assert [str(i) for i in sessions] == ['1', '2']
def test_run_manifest_abort_on_first_failure(): # Set up a valid manifest. config = Namespace(stop_on_first_error=True) sessions_ = [ mock.Mock(spec=sessions.SessionRunner), mock.Mock(spec=sessions.SessionRunner), ] manifest = Manifest({}, config) manifest._queue = copy.copy(sessions_) # Ensure each of the mocks returns a successful result. for mock_session in sessions_: mock_session.execute.return_value = sessions.Result( session=mock_session, status=sessions.Status.FAILED, ) # Run the manifest. results = tasks.run_manifest(manifest, global_config=config) # Verify the results look correct. assert len(results) == 1 assert isinstance(results[0], sessions.Result) assert results[0].session == sessions_[0] assert results[0].status == sessions.Status.FAILED # Verify that only the first session was called. assert sessions_[0].execute.called assert not sessions_[1].execute.called
def test_run_manifest(): # Set up a valid manifest. config = Namespace(stop_on_first_error=False) sessions_ = [ mock.Mock(spec=sessions.SessionRunner), mock.Mock(spec=sessions.SessionRunner), ] manifest = Manifest({}, config) manifest._queue = copy.copy(sessions_) # Ensure each of the mocks returns a successful result for mock_session in sessions_: mock_session.execute.return_value = sessions.Result( session=mock_session, status=sessions.Status.SUCCESS, ) # Run the manifest. results = tasks.run_manifest(manifest, global_config=config) # Verify the results look correct. assert len(results) == 2 assert results[0].session == sessions_[0] assert results[1].session == sessions_[1] for result in results: assert isinstance(result, sessions.Result) assert result.status == sessions.Status.SUCCESS
def test_filter_manifest_not_found(): config = Namespace(sessions=('baz', ), keywords=()) manifest = Manifest({ 'foo': session_func, 'bar': session_func, }, config) return_value = tasks.filter_manifest(manifest, config) assert return_value == 3
def test_verify_manifest_nonempty(): config = Namespace(sessions=(), keywords=()) manifest = Manifest({'session': session_func}, config) return_value = tasks.verify_manifest_nonempty( manifest, global_config=config, ) assert return_value == manifest
def test_filter_manifest(): config = Namespace(sessions=(), keywords=()) manifest = Manifest({ 'foo': session_func, 'bar': session_func, }, config) return_value = tasks.filter_manifest(manifest, config) assert return_value is manifest assert len(manifest) == 2
def test_result_serialize(): result = nox.sessions.Result( session=Namespace(signature='siggy', name='namey', func=mock.Mock()), status=nox.sessions.Status.SUCCESS, ) answer = result.serialize() assert answer['name'] == 'namey' assert answer['result'] == 'success' assert answer['result_code'] == 1 assert answer['signature'] == 'siggy'
def make_session_and_runner(self): runner = nox.sessions.SessionRunner( name='test', signature='test', func=mock.sentinel.func, global_config=Namespace(posargs=mock.sentinel.posargs), manifest=mock.create_autospec(nox.manifest.Manifest)) runner.venv = mock.create_autospec(nox.virtualenv.VirtualEnv) runner.venv.env = {} return nox.sessions.Session(runner=runner), runner
def test_filter_manifest_keywords(): config = Namespace(sessions=(), keywords='foo or bar') manifest = Manifest( { 'foo': mock.Mock(spec=()), 'bar': mock.Mock(spec=()), 'baz': mock.Mock(spec=()), }, config) return_value = tasks.filter_manifest(manifest, config) assert return_value is manifest assert len(manifest) == 2
def make_runner(self): func = mock.Mock() func.python = None func.reuse_venv = False runner = nox.sessions.SessionRunner( name='test', signature='test(1, 2)', func=func, global_config=Namespace(noxfile=os.path.join( os.getcwd(), 'nox.py'), envdir='envdir', posargs=mock.sentinel.posargs, reuse_existing_virtualenvs=False), manifest=mock.create_autospec(nox.manifest.Manifest)) return runner
def test_load_nox_module_not_found(): config = Namespace(noxfile='bogus.py') assert tasks.load_nox_module(config) == 2
def test_load_nox_module(): config = Namespace(noxfile=os.path.join(RESOURCES, 'noxfile.py')) noxfile_module = tasks.load_nox_module(config) assert noxfile_module.SIGIL == '123'
def test_create_report_noop(): config = Namespace(report=None) with mock.patch.object(io, 'open', autospec=True) as open_: results = tasks.create_report(mock.sentinel.RESULTS, config) assert not open_.called assert results is mock.sentinel.RESULTS
def test_honor_list_request_noop(): config = Namespace(list_sessions=False) manifest = {'thing': mock.sentinel.THING} return_value = tasks.honor_list_request(manifest, global_config=config) assert return_value is manifest
def test_honor_list_request(): config = Namespace(list_sessions=True) manifest = [Namespace(signature='foo')] return_value = tasks.honor_list_request(manifest, global_config=config) assert return_value == 0