def test_next_id_overflow(): try: prev_id = "9999" next_id = lexid.next_id(prev_id) assert False, (prev_id, "->", next_id) except OverflowError: pass
def test_next_id_random(): for _ in range(1000): prev_id = str(random.randint(1, 100 * 1000)) try: next_id = lexid.next_id(prev_id) assert prev_id < next_id except OverflowError: assert len(prev_id) == prev_id.count("9")
def incr( old_version: str, raw_pattern: str = "{pycalver}", *, major: bool = False, minor: bool = False, patch: bool = False, tag: typ.Optional[str] = None, tag_num: bool = False, pin_date: bool = False, maybe_date: typ.Optional[dt.date] = None, ) -> typ.Optional[str]: """Increment version string. 'old_version' is assumed to be a string that matches 'pattern' """ try: old_vinfo = parse_version_info(old_version, raw_pattern) except version.PatternError as ex: logger.error(str(ex)) return None date = version.TODAY if maybe_date is None else maybe_date cur_cinfo = _ver_to_cal_info(old_vinfo) if pin_date else cal_info(date) if _is_cal_gt(old_vinfo, cur_cinfo): logger.warning( f"Old version appears to be from the future '{old_version}'") cur_vinfo = old_vinfo else: cur_vinfo = old_vinfo._replace(**cur_cinfo._asdict()) cur_vinfo = cur_vinfo._replace(bid=lexid.next_id(cur_vinfo.bid)) if major: cur_vinfo = cur_vinfo._replace(major=cur_vinfo.major + 1, minor=0, patch=0) if minor: cur_vinfo = cur_vinfo._replace(minor=cur_vinfo.minor + 1, patch=0) if patch: cur_vinfo = cur_vinfo._replace(patch=cur_vinfo.patch + 1) if tag_num: raise NotImplementedError( "--tag-num not supported for old style patterns") if tag: cur_vinfo = cur_vinfo._replace(tag=tag) new_version = format_version(cur_vinfo, raw_pattern) if new_version == old_version: logger.error("Invalid arguments or pattern, version did not change.") return None else: return new_version
def _incr_numeric( raw_pattern: str, old_vinfo: version.V2VersionInfo, cur_vinfo: version.V2VersionInfo, major: bool, minor: bool, patch: bool, tag: typ.Optional[str], tag_num: bool, ) -> version.V2VersionInfo: """Increment (and reset to zero) non CalVer parts. >>> raw_pattern = 'MAJOR.MINOR.PATCH[PYTAGNUM]' >>> old_vinfo = parse_field_values_to_vinfo({'major': "1", 'minor': "2", 'patch': "3"}) >>> (old_vinfo.major, old_vinfo.minor, old_vinfo.patch, old_vinfo.tag, old_vinfo.pytag, old_vinfo.num) (1, 2, 3, 'final', '', 0) >>> new_vinfo = _incr_numeric( ... raw_pattern, ... old_vinfo, ... old_vinfo, ... major=False, ... minor=False, ... patch=True, ... tag='beta', ... tag_num=False, ... ) >>> (new_vinfo.major, new_vinfo.minor, new_vinfo.patch, new_vinfo.tag, new_vinfo.pytag, new_vinfo.num) (1, 2, 4, 'beta', 'b', 0) """ if major: cur_vinfo = cur_vinfo._replace(major=cur_vinfo.major + 1) if minor: cur_vinfo = cur_vinfo._replace(minor=cur_vinfo.minor + 1) if patch: cur_vinfo = cur_vinfo._replace(patch=cur_vinfo.patch + 1) if tag_num: cur_vinfo = cur_vinfo._replace(num=cur_vinfo.num + 1) if tag: if tag != cur_vinfo.tag: cur_vinfo = cur_vinfo._replace(num=0) cur_vinfo = cur_vinfo._replace(tag=tag) pytag = version.PEP440_TAG_BY_TAG[tag] cur_vinfo = cur_vinfo._replace(pytag=pytag) cur_vinfo = cur_vinfo._replace(inc0=cur_vinfo.inc0 + 1) cur_vinfo = cur_vinfo._replace(inc1=cur_vinfo.inc1 + 1) # prevent truncation of leading zeros if int(cur_vinfo.bid) < 1000: cur_vinfo = cur_vinfo._replace(bid=str(int(cur_vinfo.bid) + 1000)) cur_vinfo = cur_vinfo._replace(bid=lexid.next_id(cur_vinfo.bid)) return _reset_rollover_fields(raw_pattern, old_vinfo, cur_vinfo)
def main(start_id: str = "1001", num: int = 1, debug: bool = False) -> None: """Increment a lexid.""" if debug: print(f"{'lexical':<13} {'numerical':>12}") _curr_id = start_id for _ in range(num): try: _next_id = next_id(_curr_id) except OverflowError as err: sys.stderr.write(f"OverflowError: {err}") sys.exit(1) if debug: print(f"{_next_id:<13} {ord_val(_next_id):>12}") else: print(_next_id) _curr_id = _next_id
def test_next_id_basic(): assert lexid.next_id("01") == "02" assert lexid.next_id("09") == "110"