From c46d80b8be510fd3a6897e16edcf2105aaaf6500 Mon Sep 17 00:00:00 2001 From: yash-nisar Date: Sun, 6 Aug 2017 12:50:13 +0530 Subject: [PATCH] TravisLintBear: Check for internet connection Add a check for internet connection in the `check_prerequisites` method without which the bear will fail to run. Fixes https://github.com/coala/coala-bears/issues/1978 --- bears/yaml/TravisLintBear.py | 25 +++++++++++++++++++++++++ tests/yaml/TravisLintBearTest.py | 18 ++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/bears/yaml/TravisLintBear.py b/bears/yaml/TravisLintBear.py index f6400403b3..be582a90c1 100644 --- a/bears/yaml/TravisLintBear.py +++ b/bears/yaml/TravisLintBear.py @@ -1,3 +1,5 @@ +import requests + from coalib.bearlib.abstractions.Linter import linter from dependency_management.requirements.GemRequirement import GemRequirement @@ -24,6 +26,29 @@ class TravisLintBear: LICENSE = 'AGPL-3.0' CAN_DETECT = {'Formatting', 'Syntax'} SEE_MORE = 'https://docs.travis-ci.com/user/travis-lint' + CHECK_CONNECTION_URL = 'https://travis-ci.org/' + + @classmethod + def check_prerequisites(cls): # pragma: no cover + url_status = cls.get_status_of_url(cls.CHECK_CONNECTION_URL) + base_check = super().check_prerequisites() + if base_check is not True: + return base_check + try: + if url_status is None: + return 'You are not connected to the internet.' + else: + url_status.raise_for_status() + return True + except requests.exceptions.HTTPError: + return 'Failed to establish a connection to the server.' + + @staticmethod + def get_status_of_url(url): + try: + return requests.head(url, allow_redirects=False) + except requests.exceptions.RequestException: + return None @staticmethod def create_arguments(filename, file, config_file): diff --git a/tests/yaml/TravisLintBearTest.py b/tests/yaml/TravisLintBearTest.py index 914badbf6a..aef779d392 100644 --- a/tests/yaml/TravisLintBearTest.py +++ b/tests/yaml/TravisLintBearTest.py @@ -1,4 +1,6 @@ import os +import requests +import requests_mock from queue import Queue from coalib.results.Result import Result @@ -71,3 +73,19 @@ def test_empty_file(self): file=get_testfile_path(file_name), severity=RESULT_SEVERITY.NORMAL)], filename=get_testfile_path(file_name)) + + def test_check_prerequisites(self): + with requests_mock.Mocker() as m: + m.head(TravisLintBear.CHECK_CONNECTION_URL, + status_code=200) + self.assertTrue(TravisLintBear.check_prerequisites()) + + m.head(TravisLintBear.CHECK_CONNECTION_URL, + exc=requests.exceptions.RequestException) + self.assertEqual(TravisLintBear.check_prerequisites(), + 'You are not connected to the internet.') + + m.head(TravisLintBear.CHECK_CONNECTION_URL, + status_code=404) + self.assertEqual(TravisLintBear.check_prerequisites(), + 'Failed to establish a connection to the server.')