def test_logs_on_schema_extraction(self):
        workdir = self.useFixture(fixtures.TempDir())
        fake_logger = self.useFixture(fixtures.FakeLogger())

        good_path = os.path.join(workdir.path, 'my.py')
        with open(good_path, 'w') as f:
            f.write(
                dedent('''
                service = AcceptableService('vendor')

                root_api = service.api('/', 'root')

                @root_api.view(introduced_at='1.0')
                def my_view():
                    """Documentation."""
                '''))
        [schema] = _build_doubles.extract_schemas_from_file(good_path)

        self.assertEqual('root', schema.view_name)
        self.assertEqual('1.0', schema.version)
        self.assertEqual(['GET'], schema.methods)
        self.assertEqual('Documentation.', schema.doc)
        self.assertEqual(None, schema.input_schema)
        self.assertEqual(None, schema.output_schema)

        self.assertThat(fake_logger.output,
                        Contains('Extracting schemas from %s' % good_path))
        self.assertThat(fake_logger.output, Contains('Extracted 1 schema'))
    def test_logs_on_missing_file(self):
        workdir = self.useFixture(fixtures.TempDir())
        fake_logger = self.useFixture(fixtures.FakeLogger())

        bad_path = os.path.join(workdir.path, 'path_does_not_exist')
        result = _build_doubles.extract_schemas_from_file(bad_path)

        self.assertIsNone(result)
        self.assertThat(fake_logger.output,
                        Contains('Extracting schemas from %s' % bad_path))
        self.assertThat(
            fake_logger.output,
            Contains('Cannot extract schemas: No such file or directory'))
    def test_logs_on_no_permissions(self):
        workdir = self.useFixture(fixtures.TempDir())
        fake_logger = self.useFixture(fixtures.FakeLogger())

        bad_path = os.path.join(workdir.path, 'path_not_readable')
        with open(bad_path, 'w') as f:
            f.write("# You can't read me")
        os.chmod(bad_path, 0)
        result = _build_doubles.extract_schemas_from_file(bad_path)

        self.assertIsNone(result)
        self.assertThat(fake_logger.output,
                        Contains('Extracting schemas from %s' % bad_path))
        self.assertThat(fake_logger.output,
                        Contains('Cannot extract schemas: Permission denied'))
    def test_logs_on_syntax_error(self):
        workdir = self.useFixture(fixtures.TempDir())
        fake_logger = self.useFixture(fixtures.FakeLogger())

        bad_path = os.path.join(workdir.path, 'foo.py')
        with open(bad_path, 'w') as f:
            f.write("not valid pyton")

        result = _build_doubles.extract_schemas_from_file(bad_path)

        self.assertIsNone(result)
        self.assertThat(fake_logger.output,
                        Contains('Extracting schemas from %s' % bad_path))
        self.assertThat(
            fake_logger.output,
            Contains(
                'Cannot extract schemas: invalid syntax (foo.py, line 1)'))