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 coala#1978
  • Loading branch information
yash-nisar committed Aug 6, 2017
1 parent 92b1f28 commit 6719c14
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
22 changes: 22 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 Down Expand Up @@ -25,6 +27,26 @@ 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)
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
12 changes: 12 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,13 @@ 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 6719c14

Please sign in to comment.