Skip to content

Commit

Permalink
duplicate IDs test
Browse files Browse the repository at this point in the history
  • Loading branch information
shlomif committed Nov 28, 2023
1 parent 7c81139 commit da8dd69
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 1 deletion.
30 changes: 30 additions & 0 deletions fc-solve/site/wml/Tests/duplicate_ids.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2019 Shlomi Fish <shlomif@cpan.org>
#
# Distributed under terms of the MIT license.

import unittest

import html_unit_test
from html_unit_test import HtmlCheckDuplicateIDs


class MyTests(html_unit_test.TestCase):
def test_main(self):
ret, dups = HtmlCheckDuplicateIDs().check_dir_tree(
root="dest/"
)
self.assertTrue(ret, "HtmlCheckDuplicateIDs")
ret, dups = HtmlCheckDuplicateIDs().check_dir_tree(
root="dest-prod/"
)
self.assertTrue(ret, "HtmlCheckDuplicateIDs")


if __name__ == '__main__':
from pycotap import TAPTestRunner
suite = unittest.TestLoader().loadTestsFromTestCase(MyTests)
TAPTestRunner().run(suite)
108 changes: 108 additions & 0 deletions fc-solve/site/wml/Tests/lib/html_unit_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2020 Shlomi Fish < https://www.shlomifish.org/ >
#
# Licensed under the terms of the MIT license.

"""
"""

import os
from os.path import join
import unittest

from lxml import html


class HtmlTestsDocQuery:
"""Single query results"""
def __init__(self, doc, xpath_results):
self.doc = doc
self.xpath_results = xpath_results

def __len__(self):
"""implementing len(self)"""
return len(self.xpath_results)


class HtmlTestsDoc:
"""A single HTML document wrapper"""
def __init__(self, harness, fn):
self.harness = harness
if isinstance(fn, dict):
assert fn['type'] == 'text'
self.fn = fn['fn']
self.root = html.document_fromstring(html=fn['text'])
return
self.fn = fn
self.root = html.parse(fn)

def xpath(self, xpath_s):
return HtmlTestsDocQuery(self, self.root.xpath(xpath_s))

def has_count(self, xpath_s, count_, blurb=""):
"""is the length of xpath_s’s results count_"""
self.harness.assertEqual(len(self.xpath(xpath_s)), count_, blurb)

def has_one(self, xpath_s, blurb=""):
"""is the length of xpath_s’s results 1"""
self.has_count(xpath_s=xpath_s, count_=1, blurb=blurb)

def has_none(self, xpath_s, blurb=""):
"""is the length of xpath_s’s results 0"""
self.has_count(xpath_s=xpath_s, count_=0, blurb=blurb)


class TestCase(unittest.TestCase):
def doc(self, path):
return HtmlTestsDoc(self, path)


def _find_htmls(root):
"""find X/HTML files in 'root'"""
for dirpath, _, fns in os.walk(root):
for fn in fns:
if fn.endswith(('.html', '.xhtml')):
path = join(dirpath, fn)
yield path


def _test_finder():
for x in _find_htmls(root="dest/post-incs/t2/"):
print(x)


# _test_finder()


class HtmlCheckDuplicateIDs:
def check_dir_tree(self, root):
return self.check_files(
paths_list=_find_htmls(root=root)
)

def check_files(self, paths_list):
offending_files = {}
for input_fn in paths_list:
# doc = self.doc(input_fn)
doc = HtmlTestsDoc(None, input_fn)
ids_list = doc.xpath('//*/@id').xpath_results
found = set()
dups = set()
for x in ids_list:
if x in found:
# print("{}: [ {} ]".format(input_fn, x))
dups.add(x)
else:
found.add(x)
if len(dups) > 0:
print("DUPLICATES! {}:".format(input_fn), sorted(list(dups)))
offending_files[input_fn] = dups
# raise BaseException(x)
# print("{}:".format(input_fn), sorted(list(ids_list)))
ret = (len(offending_files) == 0)
assert ret
return ret, offending_files
5 changes: 4 additions & 1 deletion fc-solve/site/wml/lib/make/main.mak
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
LATEMP_ROOT_SOURCE_DIR := .
LATEMP_ABS_ROOT_SOURCE_DIR := $(realpath $(LATEMP_ROOT_SOURCE_DIR))

all: real_all

include lib/make/shlomif_common.mak
Expand Down Expand Up @@ -418,7 +421,7 @@ upload_temp: all
upload_local: all
$(RSYNC) -a $(D)/ $(BASE_LOCAL_UPLOAD_PREFIX)/$(TESTING_ENV__URL_SUFFIX)

TEST_ENV = SKIP_EMCC="$(SKIP_EMCC)"
TEST_ENV = PYTHONPATH="$${PYTHONPATH}:$(LATEMP_ABS_ROOT_SOURCE_DIR)/Tests/lib" SKIP_EMCC="$(SKIP_EMCC)"
TEST_TARGETS = Tests/*.{py,t}

clean:
Expand Down

0 comments on commit da8dd69

Please sign in to comment.