Permalink
Browse files

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 6, 2017
1 parent 112fa7d commit f51e54d11477f38364830f568fad969960a81972
Showing with 59 additions and 0 deletions.
  1. +28 −0 bears/yaml/TravisLintBear.py
  2. +31 −0 tests/yaml/TravisLintBearTest.py
@@ -1,3 +1,5 @@
import requests
from coalib.bearlib.abstractions.Linter import linter
from dependency_management.requirements.GemRequirement import GemRequirement
@@ -25,6 +27,32 @@ class TravisLintBear:
CAN_DETECT = {'Formatting', 'Syntax'}
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
def create_arguments(filename, file, config_file):
return 'lint', filename
@@ -1,5 +1,8 @@
import os
import requests
import requests_mock
from queue import Queue
from unittest.mock import patch
from coalib.results.Result import Result
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
@@ -71,3 +74,31 @@ 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:
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.