test for relative links adheres to npm run lint

This commit is contained in:
joeydlee95 2017-12-13 19:06:16 -08:00
parent a4db8e1c55
commit ac2caef37f

View file

@ -13,23 +13,24 @@ def main():
os.chdir(SOURCE_ROOT) os.chdir(SOURCE_ROOT)
filepaths = [] filepaths = []
totalDirs = 0
try: try:
for root, dirs, files in os.walk(DOCS_DIR): for root, dirs, files in os.walk(DOCS_DIR):
for file in files: totalDirs += len(dirs)
if file.endswith('.md'): for f in files:
filepaths.append(os.path.join(root, file)) if f.endswith('.md'):
filepaths.append(os.path.join(root, f))
except KeyboardInterrupt: except KeyboardInterrupt:
print('Keyboard interruption. Please try again.') print('Keyboard interruption. Please try again.')
return return
except:
print('Error: Could not read files in directories.')
return
totalBrokenLinks = 0 totalBrokenLinks = 0
for path in filepaths: for path in filepaths:
totalBrokenLinks += getBrokenLinks(path) totalBrokenLinks += getBrokenLinks(path)
print('Parsed through ' + str(len(filepaths)) + ' files.') print('Parsed through ' + str(len(filepaths)) +
' files within docs directory and its ' +
str(totalDirs) + ' subdirectories.')
print('Found ' + str(totalBrokenLinks) + ' broken relative links.') print('Found ' + str(totalBrokenLinks) + ' broken relative links.')
@ -38,14 +39,12 @@ def getBrokenLinks(filepath):
brokenLinks = [] brokenLinks = []
try: try:
file = open(filepath, 'r') f = open(filepath, 'r')
lines = file.readlines() lines = f.readlines()
except KeyboardInterrupt: except KeyboardInterrupt:
print('Keyboard interruption whle parsing. Please try again.') print('Keyboard interruption whle parsing. Please try again.')
except:
print('Error: Could not open file ', filepath)
finally: finally:
file.close() f.close()
regexLink = re.compile('\[(.*?)\]\((?P<links>(.*?))\)') regexLink = re.compile('\[(.*?)\]\((?P<links>(.*?))\)')
links = [] links = []
@ -60,7 +59,7 @@ def getBrokenLinks(filepath):
sections = link.split('#') sections = link.split('#')
if len(sections) > 1: if len(sections) > 1:
if str(link).startswith('#'): if str(link).startswith('#'):
if not checkSections(sections, link, lines, filepath): if not checkSections(sections, lines):
brokenLinks.append(link) brokenLinks.append(link)
else: else:
tempFile = os.path.join(currentDir, sections[0]) tempFile = os.path.join(currentDir, sections[0])
@ -70,12 +69,10 @@ def getBrokenLinks(filepath):
newLines = newFile.readlines() newLines = newFile.readlines()
except KeyboardInterrupt: except KeyboardInterrupt:
print('Keyboard interruption whle parsing. Please try again.') print('Keyboard interruption whle parsing. Please try again.')
except:
print('Error: Could not open file ', filepath)
finally: finally:
newFile.close() newFile.close()
if not checkSections(sections, link, newLines, tempFile): if not checkSections(sections, newLines):
brokenLinks.append(link) brokenLinks.append(link)
else: else:
brokenLinks.append(link) brokenLinks.append(link)
@ -88,7 +85,7 @@ def getBrokenLinks(filepath):
return len(brokenLinks) return len(brokenLinks)
def checkSections(sections, link, lines, path): def checkSections(sections, lines):
sectionHeader = sections[1].replace('-', '') sectionHeader = sections[1].replace('-', '')
regexSectionTitle = re.compile('# (?P<header>.*)') regexSectionTitle = re.compile('# (?P<header>.*)')
for line in lines: for line in lines: