Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 #1978
  • Loading branch information
yash-nisar committed Aug 10, 2017
1 parent 92b1f28 commit c46d80b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
25 changes: 25 additions & 0 deletions bears/yaml/TravisLintBear.py
@@ -1,3 +1,5 @@
import requests

from coalib.bearlib.abstractions.Linter import linter

from dependency_management.requirements.GemRequirement import GemRequirement
Expand All @@ -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):
Expand Down
18 changes: 18 additions & 0 deletions 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
Expand Down Expand Up @@ -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.')

0 comments on commit c46d80b

Please sign in to comment.