Skip to content

Commit

Permalink
TravisLintBear: Check for internet connection
Browse files Browse the repository at this point in the history
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 17, 2017
1 parent 112fa7d commit f51e54d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
28 changes: 28 additions & 0 deletions bears/yaml/TravisLintBear.py
@@ -1,3 +1,5 @@
import requests

from coalib.bearlib.abstractions.Linter import linter from coalib.bearlib.abstractions.Linter import linter


from dependency_management.requirements.GemRequirement import GemRequirement from dependency_management.requirements.GemRequirement import GemRequirement
Expand Down Expand Up @@ -25,6 +27,32 @@ class TravisLintBear:
CAN_DETECT = {'Formatting', 'Syntax'} CAN_DETECT = {'Formatting', 'Syntax'}
SEE_MORE = 'https://docs.travis-ci.com/user/travis-lint' SEE_MORE = 'https://docs.travis-ci.com/user/travis-lint'


@classmethod
def check_prerequisites(cls):
base_check = super().check_prerequisites()
if base_check is not True:
return base_check

check_connection_url = 'https://travis-ci.org/'
url_status = cls.get_url_status(check_connection_url)

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 {}.'.format(
check_connection_url)

@staticmethod
def get_url_status(url):
try:
return requests.head(url, allow_redirects=False)
except requests.exceptions.RequestException:
return None

@staticmethod @staticmethod
def create_arguments(filename, file, config_file): def create_arguments(filename, file, config_file):
return 'lint', filename return 'lint', filename
31 changes: 31 additions & 0 deletions tests/yaml/TravisLintBearTest.py
@@ -1,5 +1,8 @@
import os import os
import requests
import requests_mock
from queue import Queue from queue import Queue
from unittest.mock import patch


from coalib.results.Result import Result from coalib.results.Result import Result
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
Expand Down Expand Up @@ -71,3 +74,31 @@ def test_empty_file(self):
file=get_testfile_path(file_name), file=get_testfile_path(file_name),
severity=RESULT_SEVERITY.NORMAL)], severity=RESULT_SEVERITY.NORMAL)],
filename=get_testfile_path(file_name)) filename=get_testfile_path(file_name))

def test_check_prerequisites(self):
with requests_mock.Mocker() as m:
check_connection_url = 'https://travis-ci.org/'
m.head(check_connection_url,
status_code=200)
self.assertEqual(TravisLintBear.check_prerequisites(), True)

m.head(check_connection_url,
exc=requests.exceptions.RequestException)
self.assertEqual(TravisLintBear.check_prerequisites(),
'You are not connected to the internet.')

m.head(check_connection_url,
status_code=404)
self.assertEqual(TravisLintBear.check_prerequisites(),
'Failed to establish a connection to '
'https://travis-ci.org/.')

# The primary base class is not the `LinterBase` inside `@linter`,
# but the class the user writes because of this mixin-technique
# `@linter` uses.
with patch.object(TravisLintBear.__bases__[1],
'check_prerequisites') as mock_method:
base_check_fail_message = 'travis is not installed.'
mock_method.return_value = base_check_fail_message
self.assertEqual(base_check_fail_message,
TravisLintBear.check_prerequisites())

0 comments on commit f51e54d

Please sign in to comment.