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 6, 2017
1 parent 92b1f28 commit f0d2772
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
28 changes: 28 additions & 0 deletions bears/yaml/TravisLintBear.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import requests
from shutil import which

from coalib.bearlib.abstractions.Linter import linter

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

# IP Address of www.google.com
CHECK_CONNECTION_URL = 'http://216.58.218.174'
DEFAULT_TIMEOUT = 15

@classmethod
def check_prerequisites(cls):
code = cls.get_status_code(
cls.CHECK_CONNECTION_URL, cls.DEFAULT_TIMEOUT)
if which('travis') is None: # pragma: no cover
return ('Please ensure that travis has been installed. Refer to'
'https://docs.travis-ci.com/user/travis-lint'
'#Command-line-Validation for more details.')
else:
return ('You are not connected to the internet.'
if code is None else True)

@staticmethod
def get_status_code(url, timeout):
try:
code = requests.head(url, allow_redirects=False,
timeout=timeout).status_code
return code
except requests.exceptions.RequestException:
pass

@staticmethod
def create_arguments(filename, file, config_file):
return 'lint', filename
13 changes: 13 additions & 0 deletions tests/yaml/TravisLintBearTest.py
Original file line number Diff line number Diff line change
@@ -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,14 @@ 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.assertTrue(TravisLintBear.check_prerequisites() ==
'You are not connected to the internet.')

0 comments on commit f0d2772

Please sign in to comment.