def basic_info(err=None): if request.method == 'POST': print("POST") tl = TechLag(package=request.form['package'], version=request.form['version'], kind=request.form["kind"]) results = tl.analyze().to_html(classes="table") else: results = "" return render_template('form.html', results=results, err=err)
def test_release_type(self): """Test whether release_type properly works""" release = TechLag.release_type("1.1.0", "2.2.1") self.assertEqual(release, RELEASE_MAJOR) release = TechLag.release_type("1.1.0", "1.2.1") self.assertEqual(release, RELEASE_MINOR) release = TechLag.release_type("1.1.1", "1.1.2") self.assertEqual(release, RELEASE_PATCH)
def test_init(self): """Test whether the attributes are initialized""" tl = TechLag(url="https://...") self.assertIsNone(tl.package) self.assertIsNone(tl.version) self.assertEqual(tl.url, "https://...") tl = TechLag(package="grunt", version="1.0.0", url="https://...") self.assertEqual(tl.package, "grunt") self.assertEqual(tl.version, "1.0.0") self.assertEqual(tl.url, "https://...")
def test_convert_version(self): """Test whether convert_version properly works""" value = TechLag.convert_version("1.1.1") self.assertEqual(value, 1001001) value = TechLag.convert_version("1.1") self.assertEqual(value, 1001000) value = TechLag.convert_version("1") self.assertEqual(value, 1000000) with self.assertRaises(TechLagError): _ = TechLag.convert_version("")
def test_semserver(self): """Test whether semserver properly works""" versions = TechLag.semver(">1.2.3", ["1.1.0", "1.2.0", "1.3.0", "1.4.0"]) self.assertEqual(["1.3.0", "1.4.0"], versions) with self.assertLogs(logger, level='INFO') as cm: versions = TechLag.semver(">1.2.3", ["1.1.0", "1.2.0"]) self.assertEqual([], versions) self.assertEqual( cm.output[0], "WARNING:techlag.techlag:No package versions found for constraint " ">1.2.3 and versions ['1.1.0', '1.2.0']") with self.assertRaises(TechLagError): _ = TechLag.semver(None, [])
def test_fetch_from_url(self): """Test whether fetch_from_url properly works""" url = "https://raw.githubusercontent.com/jasmine/jasmine/master/package.json" content = read_file('data/package.json', 'rb') httpretty.register_uri(httpretty.GET, url, body=content, status=200) response = TechLag.fetch_from_url(url) self.assertEqual(response['name'], 'jasmine-core') self.assertEqual(response['version'], '3.2.1') self.assertIn('devDependencies', response)
def compute_lag(versions, used, latest): """Compute the technical lag for the set of versions of a given package. :param versions: pandas data frame of the package versions :param used: version in use of the package :param latest: latest version available of the package :return: the technical lag """ used = TechLag.convert_version(used.split('-')[0]) latest = TechLag.convert_version(latest.split('-')[0]) if used == latest: return {RELEASE_MAJOR: 0, RELEASE_MINOR: 0, RELEASE_PATCH: 0} versions['version'] = versions['version'].apply( lambda x: x.split('-')[0]) versions = versions.drop_duplicates() versions['version_count'] = versions['version'].apply( lambda x: TechLag.convert_version(x)) versions.sort_values('version_count', ascending=True, inplace=True) versions['version_old'] = versions['version'].shift(1) versions['release_type'] = versions.apply( lambda d: TechLag.release_type(d['version_old'], d['version']), axis=1) versions = versions.query('version_count>' + str(used) + ' and version_count <= ' + str(latest)) lag = versions.groupby('release_type').count()[['version' ]].to_dict()['version'] if RELEASE_MAJOR not in lag.keys(): lag[RELEASE_MAJOR] = 0 if RELEASE_MINOR not in lag.keys(): lag[RELEASE_MINOR] = 0 if RELEASE_PATCH not in lag.keys(): lag[RELEASE_PATCH] = 0 return lag
def analyze(self): """Calculate the technical lag for the target package""" if self.url: json = TechLag.fetch_from_url(self.url) else: version = self.semver( self.version, self.get_versions(self.package).version.tolist())[-1] url = urljoin(NPM_URL, self.package + '/' + version) json = TechLag.fetch_from_url(url) if self.dep_kind not in json: msg = "%s package.json doesn't contain information about %s" % ( json['name'], self.dep_kind) logger.error(msg) raise TechLagError(cause=msg) dependencies = self.get_dependencies(json, self.dep_kind) result = self.calculate_lags(dependencies) return result
def get_versions(package): """Get the version of a given package. :param package: target package name :return: pandas data frame of package versions """ url = urljoin(NPM_URL, package) versions = TechLag.fetch_from_url(url)['time'] versions.pop('modified') versions.pop('created') versions = (pd.DataFrame({ 'version': list(versions.keys()), 'date': list(versions.values()), 'package': package })) return versions
def test_analyze(self): """Test whether a NotImplementedError is thrown""" tl = TechLag(package="grunt", version="1.0.0", url="https://...") with self.assertRaises(NotImplementedError): tl.analyze()