Skip to content

Instantly share code, notes, and snippets.

@pjmattingly
Created July 23, 2022 10:36
Show Gist options
  • Select an option

  • Save pjmattingly/f01109efa3eb23f2ae9193dbe43b4d69 to your computer and use it in GitHub Desktop.

Select an option

Save pjmattingly/f01109efa3eb23f2ae9193dbe43b4d69 to your computer and use it in GitHub Desktop.
#find serverless dir
from pathlib import Path
serverless_dir = Path("./serverless")
if not serverless_dir.exists():
raise
#find all of the folders in the root of serverless
#except those that don't have customer-facing code
skipped_dirs = ["node_modules", ".git", "docs", ".github", "test"]
production_dirs = []
for f in serverless_dir.iterdir():
if f.is_dir() and not (f.stem in skipped_dirs):
production_dirs.append(f)
#then check all those dirs for .js files
js_files = []
for p_dir in production_dirs:
js_files.extend( list(p_dir.glob('**/*.js')) )
#then find all test files in the `test` directory
test_dir = serverless_dir / "test"
if not test_dir.exists():
raise
test_files = list(test_dir.glob('**/*.test.js'))
#then with the list of .js files, check for tests with the format: <name of js file>.test.js
#we will adopt a strategy of using sets
#the test file paths in `test_files` are of the format: 'serverless/test/<test subdir>/<some>/<path>/<name of js file>.test.js'
#this mimics the path to the file each test corresponds to; for example: 'serverless/lib/<some>/<path>/<name of js file>.js'
#so if we can transform the former, into the format of the later (changing `<name of js file>.test.js` to `<name of js file>.js`)
#and strip off the `serverless/test/<test subdir>` and `serverless` respectively
#then the path string for the js files found previously, and the transformed path string for each test should match
#therefore we can then add the transformed path strings for the tests to a set, and the path strings for the files to a different set
#and find the difference
#this should show which .js files do not have corresponding test files
#transforming the list of .js files
from pathlib import Path
import os
transformed_js_files = []
for f in js_files:
trunc = list(f.parts)[1:] #strip off `serverless`
transformed_js_files.append( Path(os.path.join(*trunc)) ) #recreate the now-truncated path
#transforming the list of test files
transformed_test_files = []
for f in test_files:
#strip off the end of string containing the suffixes for each test file
#for example: <name of test file>.test.js
#see: https://stackoverflow.com/questions/1038824/how-do-i-remove-a-substring-from-the-end-of-a-string
new_name = f.name[:-len("".join(f.suffixes))]
new_name += f.suffixes[-1] #re-add the .js to each file name
_f = f.with_name(new_name)
trunc = list(_f.parts)[3:] #strip off `serverless/test/<test subdir>`
if len(trunc) == 0: #ignore files in the root of serverless/test
continue
transformed_test_files.append( Path(os.path.join(*trunc)) ) #recreate the now-truncated path
set_transformed_js_files = set(transformed_js_files)
set_transformed_test_files = set(transformed_test_files)
diff = set_transformed_js_files - set_transformed_test_files
assert all([Path("serverless" / f).exists() for f in diff]) #sanity check, all such files exist
import json
print(json.dumps(sorted([str(f) for f in diff]), indent = 4) )
@pjmattingly
Copy link
Copy Markdown
Author

This code assumes a clone of the serverless code in a folder serverless in the same directory as the script. It should list (in JSON) all files without tests; Where the path of each such files is relative to the root of the serverless folder.

Tested with Python 3.8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment