def validate(ctx, paths): """Validates JSON file data located in PATHS.""" if not paths: raise click.UsageError('No files or directories specified.') error_count = 0 for path in paths: data = load_json_data(path) click.echo('Looking at %d items...' % len(data)) for fn, item in data: errors = validate_item(fn, item) if errors: error_count += len(errors) for err in errors: click.echo('%(fn)s: E:%(name)s:%(msg)s' % { 'fn': fn, 'name': err.name, 'msg': err.msg }, err=True) # FIXME: Validate things that need to be unique across the # dataset here. # FIXME: Validate file format? i.e. 2-space indents? Sort order? print('Done!') ctx.exit(code=1 if error_count else 0)
def validate(ctx, paths): """Validates JSON file data located in PATHS.""" if not paths: raise click.UsageError('No files or directories specified.') error_count = 0 for path in paths: data = load_json_data(path) click.echo('Looking at %d items...' % len(data)) for fn, item in data: errors = validate_item(fn, item) if errors: error_count += len(errors) for err in errors: click.echo( '%(fn)s: E:%(name)s:%(msg)s' % {'fn': fn, 'name': err.name, 'msg': err.msg}, err=True ) # FIXME: Validate things that need to be unique across the # dataset here. # FIXME: Validate file format? i.e. 2-space indents? Sort order? print('Done!') ctx.exit(code=1 if error_count else 0)
def validate(ctx, errorsonly, paths): """Validates JSON file data located in PATHS.""" if not paths: raise click.UsageError('No files or directories specified.') errors_count = 0 warnings_count = 0 for path in paths: data = load_json_data(path) click.echo('Looking at %d items...' % len(data)) for fn, item in data: results = validate_item(fn, item) if results: errors = [res for res in results if res.level == ERROR] errors_count += len(errors) for err in errors: click.echo( '%(fn)s ERROR:%(name)s:%(msg)s' % { 'fn': fn, 'name': err.name, 'msg': err.msg }, err=True ) if not errorsonly: warnings = [res for res in results if res.level == WARNING] warnings_count += len(warnings) for warn in warnings: click.echo( '%(fn)s WARN:%(name)s:%(msg)s' % { 'fn': fn, 'name': warn.name, 'msg': warn.msg } ) # FIXME: Validate things that need to be unique across the # dataset here. # FIXME: Validate file format? i.e. 2-space indents? Sort order? if not errorsonly: print('%d warnings found.' % warnings_count) print('%d errors found.' % errors_count) print('Done!') ctx.exit(code=1 if errors_count else 0)
def test_directory(self, tmpdir): cat_path = tmpdir.join('pycon').mkdir() path1 = cat_path.join('foo1.json') path1.write('{}') path2 = cat_path.join('foo2.json') path2.write('{}') path3 = tmpdir.join('djangocon').mkdir().join('foo3.json') path3.write('{}') assert ( load_json_data(tmpdir.strpath) == [ # Note: djangocon comes first because it's sorted (path3.strpath, {}), (path1.strpath, {}), (path2.strpath, {}), ] )
def test_non_json_file(self, tmpdir): path = tmpdir.join('foo.txt') path.write('test file') assert load_json_data(tmpdir.strpath) == [] assert load_json_data(path.strpath) == []
def test_bad_paths(self): assert load_json_data(None) == [] assert load_json_data('') == [] assert load_json_data('/nonexistent/file') == []
def test_json_file(self, tmpdir): path = tmpdir.join('foo.json') path.write('{}') assert load_json_data(tmpdir.strpath) == [(path.strpath, {})] assert load_json_data(path.strpath) == [(path.strpath, {})]