mdw-octoml commented on a change in pull request #7289: URL: https://github.com/apache/tvm/pull/7289#discussion_r568063457
########## File path: tests/python/unittest/test_gen_requirements.py ########## @@ -0,0 +1,220 @@ +#!/usr/bin/env python3 +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +"""Tests for gen_requirements, found in python/.""" + +import collections +import contextlib +import os +import sys + +import tvm + +import pytest + +# Insert the parent dir to python/tvm into the import path, so that gen_requirements may be +# imported. +sys.path.insert(0, os.path.dirname(tvm.__file__)) +try: + import gen_requirements +finally: + sys.path.pop(0) + + [email protected] +def patch(obj, **kw): + old = {} + for prop_name, new in kw.items(): + old[prop_name] = getattr(obj, prop_name) + setattr(obj, prop_name, new) + yield + for prop_name, value in old.items(): + setattr(obj, prop_name, value) + + +PROBLEM_REQUIREMENTS = [ + ("extras-pre-core", ("", ["foo", 123])), # entry before core + (456, ("", ["foo", "bar"])), # invalid extras name, deps should not be processed + ("core", ("", ["foo"])), # ordinary core entry. + ("wrong-description-type", (None, ["foo"])), # wrong description type + ("bad-value", None), # value field is not a 2-tuple + ("bad-value-2", ("", ["foo"], 34)), # value field is not a 2-tuple + ("invalid", ("", ["qux"])), # duplicate invalid entry, all items valid. + ("extras-foo", ("", ["bar", "baz"])), # ordinary extras entry. + ("invalid", ("", ["baz", None, 123])), # valid extra name, invalid deps. + ("unsorted", ("", ["qux", "bar", "foo"])), # deps out of order + ("versioned_dep", ("", ["baz==1.2", "foo==^2.0", "buz<3", "bar>4"])), + ("duplicate_dep", ("", ["buz", "buz", "foo"])), # duplicate listed dependency + ("dev", ("", ["baz", "qux"])), # ordinary dev entry. + ("extras-post-dev", ("", ["bar", "buzz"])), # entry after dev +] + + +def test_validate_requirements(): + with patch(gen_requirements, REQUIREMENTS_BY_PIECE=None): + assert gen_requirements.validate_requirements_by_piece() == [ + "must be list or tuple, see None" + ] + + with patch(gen_requirements, REQUIREMENTS_BY_PIECE=PROBLEM_REQUIREMENTS): + problems = gen_requirements.validate_requirements_by_piece() + assert problems == [ + 'piece extras-pre-core: must list after "core" (core must be first)', + "piece extras-pre-core: deps should be a list of strings, got ['foo', 123]", + "piece 456: must be str", + "piece wrong-description-type: description should be a string, got None", + ( + 'piece bad-value: should be formatted like ("bad-value", ("<requirements.txt ' + 'comment>", ["dep1", "dep2", ...])). got: None' + ), + ( + 'piece bad-value-2: should be formatted like ("bad-value-2", ' + '("<requirements.txt comment>", ["dep1", "dep2", ...])). got: (\'\', ' + "['foo'], 34)" + ), + "piece invalid: listed twice", + "piece invalid: deps should be a list of strings, got ['baz', None, 123]", + "piece unsorted: deps must be sorted. Correct order:\n ['bar', 'foo', 'qux']", + "piece versioned_dep: deps must be sorted. Correct order:\n ['bar>4', 'baz==1.2', 'buz<3', 'foo==^2.0']", + "piece versioned_dep: dependency baz==1.2 should not specify a version. Add it to CONSTRAINTS instead.", + "piece versioned_dep: dependency foo==^2.0 should not specify a version. Add it to CONSTRAINTS instead.", + "piece versioned_dep: dependency buz<3 should not specify a version. Add it to CONSTRAINTS instead.", + "piece versioned_dep: dependency bar>4 should not specify a version. Add it to CONSTRAINTS instead.", + "piece duplicate_dep: dependency buz listed twice", + 'piece extras-post-dev: must list before "dev" (dev must be last)', + 'pieces other than "core" and "dev" must appear in alphabetical order: ' + "['bad-value', 'bad-value-2', 'duplicate_dep', 'extras-foo', 'extras-post-dev', " + "'extras-pre-core', 'invalid', 'invalid', 'unsorted', 'versioned_dep', " + "'wrong-description-type']", + ] + + +TEST_REQUIREMENTS_BY_PIECE = ( + ("core", ("core tvm requirements", ("bar", "foo", "non-constrained"))), + ("extra-one", ("requirements for one feature", ("baz", "qux"))), + ("extra-two", ("requirements for two feature", ("buz", "qux", "semver-minor", "semver-patch"))), + ("dev", ("requirements for dev", ("buz", "oof", "rab"))), +) + + +def test_validate_constraints(): + with patch( + gen_requirements, + REQUIREMENTS_BY_PIECE=TEST_REQUIREMENTS_BY_PIECE, + CONSTRAINTS=( + ("unlisted", "~=3"), Review comment: Same comment as above, I think it would be better if the problems were kept along with the input constraints that are being tested. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
