allow list of regex patterns for hiding file names
This commit is contained in:
parent
de72db51a8
commit
7f198639aa
3 changed files with 146 additions and 31 deletions
6
tests/samples/configs/hide_file_names.cfg
Normal file
6
tests/samples/configs/hide_file_names.cfg
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
[settings]
|
||||||
|
debug = false
|
||||||
|
api_key = 1234
|
||||||
|
hidefilenames =
|
||||||
|
missingfile
|
||||||
|
twolinefile\.txt$
|
|
@ -325,7 +325,7 @@ class MainTestCase(utils.TestCase):
|
||||||
self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
|
self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
|
||||||
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
||||||
|
|
||||||
def test_hidden_filename(self):
|
def test_hide_all_filenames(self):
|
||||||
response = Response()
|
response = Response()
|
||||||
response.status_code = 0
|
response.status_code = 0
|
||||||
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
||||||
|
@ -371,6 +371,98 @@ class MainTestCase(utils.TestCase):
|
||||||
self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
|
self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
|
||||||
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
||||||
|
|
||||||
|
def test_hide_matching_filenames(self):
|
||||||
|
response = Response()
|
||||||
|
response.status_code = 0
|
||||||
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
||||||
|
|
||||||
|
with utils.TemporaryDirectory() as tempdir:
|
||||||
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
||||||
|
shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt'))
|
||||||
|
entity = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt'))
|
||||||
|
|
||||||
|
now = u(int(time.time()))
|
||||||
|
config = 'tests/samples/configs/hide_file_names.cfg'
|
||||||
|
|
||||||
|
args = ['--file', entity, '--key', '123', '--config', config, '--time', now]
|
||||||
|
|
||||||
|
retval = execute(args)
|
||||||
|
self.assertEquals(retval, API_ERROR)
|
||||||
|
self.assertEquals(sys.stdout.getvalue(), '')
|
||||||
|
self.assertEquals(sys.stderr.getvalue(), '')
|
||||||
|
|
||||||
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
||||||
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with()
|
||||||
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
|
||||||
|
|
||||||
|
heartbeat = {
|
||||||
|
'language': 'Text only',
|
||||||
|
'lines': 2,
|
||||||
|
'entity': 'HIDDEN.txt',
|
||||||
|
'project': os.path.basename(os.path.abspath('.')),
|
||||||
|
'time': float(now),
|
||||||
|
'type': 'file',
|
||||||
|
}
|
||||||
|
stats = {
|
||||||
|
u('cursorpos'): None,
|
||||||
|
u('dependencies'): [],
|
||||||
|
u('language'): u('Text only'),
|
||||||
|
u('lineno'): None,
|
||||||
|
u('lines'): 2,
|
||||||
|
}
|
||||||
|
|
||||||
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None)
|
||||||
|
for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items():
|
||||||
|
self.assertEquals(heartbeat[key], val)
|
||||||
|
self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
|
||||||
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
||||||
|
|
||||||
|
def test_does_not_hide_unmatching_filenames(self):
|
||||||
|
response = Response()
|
||||||
|
response.status_code = 0
|
||||||
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
||||||
|
|
||||||
|
with utils.TemporaryDirectory() as tempdir:
|
||||||
|
entity = 'tests/samples/codefiles/emptyfile.txt'
|
||||||
|
shutil.copy(entity, os.path.join(tempdir, 'emptyfile.txt'))
|
||||||
|
entity = os.path.realpath(os.path.join(tempdir, 'emptyfile.txt'))
|
||||||
|
|
||||||
|
now = u(int(time.time()))
|
||||||
|
config = 'tests/samples/configs/hide_file_names.cfg'
|
||||||
|
|
||||||
|
args = ['--file', entity, '--key', '123', '--config', config, '--time', now]
|
||||||
|
|
||||||
|
retval = execute(args)
|
||||||
|
self.assertEquals(retval, API_ERROR)
|
||||||
|
self.assertEquals(sys.stdout.getvalue(), '')
|
||||||
|
self.assertEquals(sys.stderr.getvalue(), '')
|
||||||
|
|
||||||
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
||||||
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with()
|
||||||
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
|
||||||
|
|
||||||
|
heartbeat = {
|
||||||
|
'language': 'Text only',
|
||||||
|
'lines': 0,
|
||||||
|
'entity': entity,
|
||||||
|
'project': os.path.basename(os.path.abspath('.')),
|
||||||
|
'time': float(now),
|
||||||
|
'type': 'file',
|
||||||
|
}
|
||||||
|
stats = {
|
||||||
|
u('cursorpos'): None,
|
||||||
|
u('dependencies'): [],
|
||||||
|
u('language'): u('Text only'),
|
||||||
|
u('lineno'): None,
|
||||||
|
u('lines'): 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None)
|
||||||
|
for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items():
|
||||||
|
self.assertEquals(heartbeat[key], val)
|
||||||
|
self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
|
||||||
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
||||||
|
|
||||||
def test_invalid_timeout_passed_via_command_line(self):
|
def test_invalid_timeout_passed_via_command_line(self):
|
||||||
response = Response()
|
response = Response()
|
||||||
response.status_code = 201
|
response.status_code = 201
|
||||||
|
|
|
@ -238,10 +238,23 @@ def parseArguments():
|
||||||
args.include.append(pattern)
|
args.include.append(pattern)
|
||||||
except TypeError: # pragma: nocover
|
except TypeError: # pragma: nocover
|
||||||
pass
|
pass
|
||||||
|
if args.hidefilenames:
|
||||||
|
args.hidefilenames = ['.*']
|
||||||
|
else:
|
||||||
|
args.hidefilenames = []
|
||||||
|
if configs.has_option('settings', 'hidefilenames'):
|
||||||
|
option = configs.get('settings', 'hidefilenames').strip()
|
||||||
|
if option.lower() == 'true':
|
||||||
|
args.hidefilenames = ['.*']
|
||||||
|
elif option.lower() != 'false':
|
||||||
|
try:
|
||||||
|
for pattern in option.split("\n"):
|
||||||
|
if pattern.strip() != '':
|
||||||
|
args.hidefilenames.append(pattern)
|
||||||
|
except TypeError:
|
||||||
|
pass
|
||||||
if args.offline and configs.has_option('settings', 'offline'):
|
if args.offline and configs.has_option('settings', 'offline'):
|
||||||
args.offline = configs.getboolean('settings', 'offline')
|
args.offline = configs.getboolean('settings', 'offline')
|
||||||
if not args.hidefilenames and configs.has_option('settings', 'hidefilenames'):
|
|
||||||
args.hidefilenames = configs.getboolean('settings', 'hidefilenames')
|
|
||||||
if not args.proxy and configs.has_option('settings', 'proxy'):
|
if not args.proxy and configs.has_option('settings', 'proxy'):
|
||||||
args.proxy = configs.get('settings', 'proxy')
|
args.proxy = configs.get('settings', 'proxy')
|
||||||
if not args.verbose and configs.has_option('settings', 'verbose'):
|
if not args.verbose and configs.has_option('settings', 'verbose'):
|
||||||
|
@ -266,7 +279,6 @@ def parseArguments():
|
||||||
|
|
||||||
def should_exclude(entity, include, exclude):
|
def should_exclude(entity, include, exclude):
|
||||||
if entity is not None and entity.strip() != '':
|
if entity is not None and entity.strip() != '':
|
||||||
try:
|
|
||||||
for pattern in include:
|
for pattern in include:
|
||||||
try:
|
try:
|
||||||
compiled = re.compile(pattern, re.IGNORECASE)
|
compiled = re.compile(pattern, re.IGNORECASE)
|
||||||
|
@ -277,9 +289,6 @@ def should_exclude(entity, include, exclude):
|
||||||
msg=u(ex),
|
msg=u(ex),
|
||||||
pattern=u(pattern),
|
pattern=u(pattern),
|
||||||
))
|
))
|
||||||
except TypeError: # pragma: nocover
|
|
||||||
pass
|
|
||||||
try:
|
|
||||||
for pattern in exclude:
|
for pattern in exclude:
|
||||||
try:
|
try:
|
||||||
compiled = re.compile(pattern, re.IGNORECASE)
|
compiled = re.compile(pattern, re.IGNORECASE)
|
||||||
|
@ -290,8 +299,6 @@ def should_exclude(entity, include, exclude):
|
||||||
msg=u(ex),
|
msg=u(ex),
|
||||||
pattern=u(pattern),
|
pattern=u(pattern),
|
||||||
))
|
))
|
||||||
except TypeError: # pragma: nocover
|
|
||||||
pass
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
@ -336,8 +343,18 @@ def send_heartbeat(project=None, branch=None, hostname=None, stats={}, key=None,
|
||||||
'type': entity_type,
|
'type': entity_type,
|
||||||
}
|
}
|
||||||
if hidefilenames and entity is not None and entity_type == 'file':
|
if hidefilenames and entity is not None and entity_type == 'file':
|
||||||
|
for pattern in hidefilenames:
|
||||||
|
try:
|
||||||
|
compiled = re.compile(pattern, re.IGNORECASE)
|
||||||
|
if compiled.search(entity):
|
||||||
extension = u(os.path.splitext(data['entity'])[1])
|
extension = u(os.path.splitext(data['entity'])[1])
|
||||||
data['entity'] = u('HIDDEN{0}').format(extension)
|
data['entity'] = u('HIDDEN{0}').format(extension)
|
||||||
|
break
|
||||||
|
except re.error as ex:
|
||||||
|
log.warning(u('Regex error ({msg}) for include pattern: {pattern}').format(
|
||||||
|
msg=u(ex),
|
||||||
|
pattern=u(pattern),
|
||||||
|
))
|
||||||
if stats.get('lines'):
|
if stats.get('lines'):
|
||||||
data['lines'] = stats['lines']
|
data['lines'] = stats['lines']
|
||||||
if stats.get('language'):
|
if stats.get('language'):
|
||||||
|
|
Loading…
Reference in a new issue