コード例 #1
0
    def test_uses_wakatime_home_env_variable(self):
        queue = Queue(None, None)

        with TemporaryDirectory() as tempdir:
            expected = os.path.realpath(os.path.join(tempdir, '.wakatime.db'))

            with mock.patch('os.environ.get') as mock_env:
                mock_env.return_value = os.path.realpath(tempdir)

                actual = queue._get_db_file()
                self.assertEquals(actual, expected)
コード例 #2
0
    def test_uses_wakatime_home_env_variable(self):
        queue = Queue(None, None)

        with TemporaryDirectory() as tempdir:
            expected = os.path.realpath(os.path.join(tempdir, '.wakatime.db'))

            with mock.patch('os.environ.get') as mock_env:
                mock_env.return_value = os.path.realpath(tempdir)

                actual = queue._get_db_file()
                self.assertEquals(actual, expected)
コード例 #3
0
    def test_leftover_heartbeats_saved_when_bulk_response_not_matching_length(self, logs):
        logging.disable(logging.NOTSET)

        with NamedTemporaryFile() as fh:
            with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file:
                mock_db_file.return_value = fh.name

                entities = [
                    'emptyfile.txt',
                    'twolinefile.txt',
                    'python.py',
                    'go.go',
                    'java.java',
                    'php.php',
                ]

                with TemporaryDirectory() as tempdir:
                    for entity in entities:
                        shutil.copy(os.path.join('tests/samples/codefiles', entity), os.path.join(tempdir, entity))

                    now = u(int(time.time()))
                    key = str(uuid.uuid4())
                    args = ['--file', os.path.join(tempdir, entities[0]), '--key', key, '--config', 'tests/samples/configs/good_config.cfg', '--time', now, '--extra-heartbeats']

                    response = CustomResponse()
                    response.response_code = 202
                    response.response_text = '{"responses": [[null,201], [null,201]]}'
                    self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response

                    with mock.patch('wakatime.main.sys.stdin') as mock_stdin:
                        heartbeats = json.dumps([{
                            'timestamp': now,
                            'entity': os.path.join(tempdir, entity),
                            'entity_type': 'file',
                            'is_write': False,
                        } for entity in entities[1:]])
                        mock_stdin.readline.return_value = heartbeats

                        retval = execute(args)
                        self.assertEquals(retval, SUCCESS)
                        self.assertNothingPrinted()

                        queue = Queue(None, None)
                        self.assertEquals(queue._get_db_file(), fh.name)
                        saved_heartbeats = queue.pop_many()
                        self.assertNothingPrinted()

                        expected = "WakaTime WARNING Missing 4 results from api.\nWakaTime WARNING Missing 2 results from api."
                        actual = self.getLogOutput(logs)
                        self.assertEquals(actual, expected)

                        # make sure extra heartbeats not matching server response were saved
                        self.assertEquals(len(saved_heartbeats), 2)
コード例 #4
0
    def test_leftover_heartbeats_saved_when_bulk_response_not_matching_length(self, logs):
        logging.disable(logging.NOTSET)

        with NamedTemporaryFile() as fh:
            with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file:
                mock_db_file.return_value = fh.name

                entities = [
                    'emptyfile.txt',
                    'twolinefile.txt',
                    'python.py',
                    'go.go',
                    'java.java',
                    'php.php',
                ]

                with TemporaryDirectory() as tempdir:
                    for entity in entities:
                        shutil.copy(os.path.join('tests/samples/codefiles', entity), os.path.join(tempdir, entity))

                    now = u(int(time.time()))
                    key = str(uuid.uuid4())
                    args = ['--file', os.path.join(tempdir, entities[0]), '--key', key, '--config', 'tests/samples/configs/good_config.cfg', '--time', now, '--extra-heartbeats']

                    response = CustomResponse()
                    response.response_code = 202
                    response.response_text = '{"responses": [[null,201], [null,201]]}'
                    self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response

                    with mock.patch('wakatime.main.sys.stdin') as mock_stdin:
                        heartbeats = json.dumps([{
                            'timestamp': now,
                            'entity': os.path.join(tempdir, entity),
                            'entity_type': 'file',
                            'is_write': False,
                        } for entity in entities[1:]])
                        mock_stdin.readline.return_value = heartbeats

                        retval = execute(args)
                        self.assertEquals(retval, SUCCESS)
                        self.assertNothingPrinted()

                        queue = Queue(None, None)
                        self.assertEquals(queue._get_db_file(), fh.name)
                        saved_heartbeats = next(queue.pop_many())
                        self.assertNothingPrinted()

                        expected = "WakaTime WARNING Missing 4 results from api.\nWakaTime WARNING Missing 2 results from api."
                        actual = self.getLogOutput(logs)
                        self.assertEquals(actual, expected)

                        # make sure extra heartbeats not matching server response were saved
                        self.assertEquals(len(saved_heartbeats), 2)
コード例 #5
0
 def test_uses_home_folder_by_default(self):
     queue = Queue(None, None)
     db_file = queue._get_db_file()
     expected = os.path.join(os.path.expanduser('~'), '.wakatime.db')
     self.assertEquals(db_file, expected)
コード例 #6
0
    def test_heartbeats_sent_not_saved_from_bulk_response(self, logs):
        logging.disable(logging.NOTSET)

        with NamedTemporaryFile() as fh:
            with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file:
                mock_db_file.return_value = fh.name

                entities = [
                    'emptyfile.txt',
                    'twolinefile.txt',
                    'python.py',
                    'go.go',
                ]

                with TemporaryDirectory() as tempdir:
                    for entity in entities:
                        shutil.copy(os.path.join('tests/samples/codefiles', entity), os.path.join(tempdir, entity))

                    now = u(int(time.time()))
                    key = str(uuid.uuid4())
                    args = ['--file', os.path.join(tempdir, entities[0]), '--key', key, '--config', 'tests/samples/configs/good_config.cfg', '--time', now, '--extra-heartbeats']

                    response = CustomResponse()
                    response.response_code = 202
                    response.response_text = '{"responses": [[null,201], [null,500], [null,201], [null, 500]]}'
                    self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response

                    with mock.patch('wakatime.main.sys.stdin') as mock_stdin:
                        heartbeats = json.dumps([{
                            'timestamp': now,
                            'entity': os.path.join(tempdir, entity),
                            'entity_type': 'file',
                            'is_write': False,
                        } for entity in entities[1:]])
                        mock_stdin.readline.return_value = heartbeats

                        with mock.patch('wakatime.offlinequeue.Queue.pop') as mock_pop:
                            mock_pop.return_value = None

                            retval = execute(args)

                        self.assertEquals(retval, SUCCESS)
                        self.assertNothingPrinted()

                        heartbeat = {
                            'entity': os.path.realpath(os.path.join(tempdir, entities[0])),
                            'language': ANY,
                            'lines': ANY,
                            'project': ANY,
                            'time': ANY,
                            'type': 'file',
                            'is_write': ANY,
                            'user_agent': ANY,
                            'dependencies': ANY,
                        }
                        extra_heartbeats = [{
                            'entity': os.path.realpath(os.path.join(tempdir, entity)),
                            'language': ANY,
                            'lines': ANY,
                            'project': ANY,
                            'branch': ANY,
                            'time': ANY,
                            'is_write': ANY,
                            'type': 'file',
                            'dependencies': ANY,
                            'user_agent': ANY,
                        } for entity in entities[1:]]
                        self.assertHeartbeatSent(heartbeat, extra_heartbeats=extra_heartbeats)

                        self.assertSessionCacheSaved()

                        queue = Queue(None, None)
                        self.assertEquals(queue._get_db_file(), fh.name)
                        saved_heartbeats = queue.pop_many()
                        self.assertNothingPrinted()
                        self.assertNothingLogged(logs)

                        # make sure only heartbeats with error code responses were saved
                        self.assertEquals(len(saved_heartbeats), 2)
                        self.assertEquals(saved_heartbeats[0].entity, os.path.realpath(os.path.join(tempdir, entities[1])))
                        self.assertEquals(saved_heartbeats[1].entity, os.path.realpath(os.path.join(tempdir, entities[3])))
コード例 #7
0
 def test_uses_home_folder_by_default(self):
     queue = Queue(None, None)
     db_file = queue._get_db_file()
     expected = os.path.join(os.path.expanduser('~'), '.wakatime.db')
     self.assertEquals(db_file, expected)
コード例 #8
0
    def test_heartbeats_sent_not_saved_from_bulk_response(self, logs):
        logging.disable(logging.NOTSET)

        with NamedTemporaryFile() as fh:
            with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file:
                mock_db_file.return_value = fh.name

                entities = [
                    'emptyfile.txt',
                    'twolinefile.txt',
                    'python.py',
                    'go.go',
                ]

                with TemporaryDirectory() as tempdir:
                    for entity in entities:
                        shutil.copy(os.path.join('tests/samples/codefiles', entity), os.path.join(tempdir, entity))

                    now = u(int(time.time()))
                    key = str(uuid.uuid4())
                    args = ['--file', os.path.join(tempdir, entities[0]), '--key', key, '--config', 'tests/samples/configs/good_config.cfg', '--time', now, '--extra-heartbeats']

                    response = CustomResponse()
                    response.response_code = 202
                    response.response_text = '{"responses": [[null,201], [null,500], [null,201], [null, 500]]}'
                    self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response

                    with mock.patch('wakatime.main.sys.stdin') as mock_stdin:
                        heartbeats = json.dumps([{
                            'timestamp': now,
                            'entity': os.path.join(tempdir, entity),
                            'entity_type': 'file',
                            'is_write': False,
                        } for entity in entities[1:]])
                        mock_stdin.readline.return_value = heartbeats

                        with mock.patch('wakatime.offlinequeue.Queue.pop') as mock_pop:
                            mock_pop.return_value = None

                            retval = execute(args)

                        self.assertEquals(retval, SUCCESS)
                        self.assertNothingPrinted()

                        heartbeat = {
                            'entity': os.path.realpath(os.path.join(tempdir, entities[0])),
                            'language': ANY,
                            'lines': ANY,
                            'project': ANY,
                            'time': ANY,
                            'type': 'file',
                            'is_write': ANY,
                            'user_agent': ANY,
                            'dependencies': ANY,
                        }
                        extra_heartbeats = [{
                            'entity': os.path.realpath(os.path.join(tempdir, entity)),
                            'language': ANY,
                            'lines': ANY,
                            'project': ANY,
                            'branch': ANY,
                            'time': ANY,
                            'is_write': ANY,
                            'type': 'file',
                            'dependencies': ANY,
                            'user_agent': ANY,
                        } for entity in entities[1:]]
                        self.assertHeartbeatSent(heartbeat, extra_heartbeats=extra_heartbeats)

                        self.assertSessionCacheSaved()

                        queue = Queue(None, None)
                        self.assertPathsEqual(queue._get_db_file(), fh.name)
                        saved_heartbeats = next(queue.pop_many())
                        self.assertNothingPrinted()
                        self.assertNothingLogged(logs)

                        # make sure only heartbeats with error code responses were saved
                        self.assertEquals(sum(1 for x in saved_heartbeats), 2)
                        self.assertPathsEqual(saved_heartbeats[0].entity, os.path.realpath(os.path.join(tempdir, entities[1])))
                        self.assertPathsEqual(saved_heartbeats[1].entity, os.path.realpath(os.path.join(tempdir, entities[3])))
コード例 #9
0
ファイル: test_offlinequeue.py プロジェクト: Aareon/wakatime
    def test_offline_heartbeats_sent_after_partial_success_from_bulk_response(
            self, logs):
        logging.disable(logging.NOTSET)

        with utils.NamedTemporaryFile() as fh:
            with utils.mock.patch('wakatime.offlinequeue.Queue._get_db_file'
                                  ) as mock_db_file:
                mock_db_file.return_value = fh.name

                entities = [
                    'emptyfile.txt',
                    'twolinefile.txt',
                    'python.py',
                    'go.go',
                ]

                with utils.TemporaryDirectory() as tempdir:
                    for entity in entities:
                        shutil.copy(
                            os.path.join('tests/samples/codefiles', entity),
                            os.path.join(tempdir, entity))

                    now = u(int(time.time()))
                    key = str(uuid.uuid4())
                    args = [
                        '--file',
                        os.path.join(tempdir, entities[0]), '--key', key,
                        '--config', 'tests/samples/configs/good_config.cfg',
                        '--time', now, '--extra-heartbeats'
                    ]

                    class CustomResponse(Response):
                        @property
                        def status_code(self):
                            return 202

                        @status_code.setter
                        def status_code(self, value):
                            pass

                        @property
                        def text(self):
                            return '[[{"id":1},201], [{"error":"error 2"},500], [{"id":3},201], [{"error":4},500]]'

                    response = CustomResponse()
                    self.patched[
                        'wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response

                    with utils.mock.patch(
                            'wakatime.main.sys.stdin') as mock_stdin:
                        heartbeats = json.dumps([{
                            'timestamp':
                            now,
                            'entity':
                            os.path.join(tempdir, entity),
                            'entity_type':
                            'file',
                            'is_write':
                            False,
                        } for entity in entities[1:]])
                        mock_stdin.readline.return_value = heartbeats

                        retval = execute(args)
                        self.assertEquals(retval, SUCCESS)
                        self.assertNothingPrinted()

                        queue = Queue(None, None)
                        self.assertEquals(queue._get_db_file(), fh.name)
                        saved_heartbeats = queue.pop_many()
                        self.assertNothingPrinted()
                        self.assertNothingLogged(logs)

                        # make sure all offline heartbeats were sent, so queue should only have 1 heartbeat left from the second 500 response
                        self.assertEquals(len(saved_heartbeats), 1)