Skip to content

Commit

Permalink
feat: exit with error if no req found in manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
ekbsnyk authored and anthogez committed Nov 9, 2020
1 parent ca70114 commit 1aa6893
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 26 deletions.
9 changes: 9 additions & 0 deletions lib/dependencies/inspect-implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,17 @@ export async function inspectInstalledDeps(
],
{ cwd: root }
);

return JSON.parse(output) as legacyCommon.DepTree;
} catch (error) {
if (typeof error === 'string') {
const emptyManifestMsg = 'No dependencies detected in manifest.';
const noDependenciesDetected = error.includes(emptyManifestMsg);

if (noDependenciesDetected) {
throw new Error(emptyManifestMsg);
}

if (error.indexOf('Required packages missing') !== -1) {
let errMsg = error;
if (path.basename(targetFile) === FILENAMES.pipenv.manifest) {
Expand All @@ -139,6 +147,7 @@ export async function inspectInstalledDeps(
throw new Error(errMsg);
}
}

throw error;
} finally {
tempDirObj.removeCallback();
Expand Down
47 changes: 27 additions & 20 deletions pysrc/pip_resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,11 @@ def get_requirements_list(requirements_file_path, dev_deps=False):
req_list = list(parsed_reqs.get('packages', []))
if dev_deps:
req_list.extend(parsed_reqs.get('dev-packages', []))
for r in req_list:
r.provenance = (requirements_file_path, r.provenance[1], r.provenance[2])
if not req_list:
return []
else:
for r in req_list:
r.provenance = (requirements_file_path, r.provenance[1], r.provenance[2])
elif os.path.basename(requirements_file_path) == 'setup.py':
with open(requirements_file_path, 'r') as f:
setup_py_file_content = f.read()
Expand Down Expand Up @@ -254,25 +257,29 @@ def create_dependencies_tree_by_req_file_path(requirements_file_path,

# create a list of dependencies from the dependencies file
required = get_requirements_list(requirements_file_path, dev_deps=dev_deps)
installed = [canonicalize_package_name(p) for p in dist_index]
top_level_requirements = []
missing_package_names = []
for r in required:
if canonicalize_package_name(r.name) not in installed:
missing_package_names.append(r.name)
else:
top_level_requirements.append(r)
if missing_package_names:
msg = 'Required packages missing: ' + (', '.join(missing_package_names))
if allow_missing:
sys.stderr.write(msg + "\n")
else:
sys.exit(msg)
if not required:
msg = 'No dependencies detected in manifest.'
sys.exit(msg)
else:
installed = [canonicalize_package_name(p) for p in dist_index]
top_level_requirements = []
missing_package_names = []
for r in required:
if canonicalize_package_name(r.name) not in installed:
missing_package_names.append(r.name)
else:
top_level_requirements.append(r)
if missing_package_names:
msg = 'Required packages missing: ' + (', '.join(missing_package_names))
if allow_missing:
sys.stderr.write(msg + "\n")
else:
sys.exit(msg)

# build a tree of dependencies
package_tree = create_tree_of_packages_dependencies(
dist_tree, top_level_requirements, requirements_file_path, allow_missing, only_provenance)
print(json.dumps(package_tree))
# build a tree of dependencies
package_tree = create_tree_of_packages_dependencies(
dist_tree, top_level_requirements, requirements_file_path, allow_missing, only_provenance)
print(json.dumps(package_tree))


def main():
Expand Down
28 changes: 22 additions & 6 deletions test/system/inspect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,12 +653,11 @@ test('Pipfile package found conditionally based on python version', (t) => {
.then(() => {
return plugin.inspect('.', 'Pipfile');
})
.then((result) => {
const pkg = result.package;
t.notOk(pkg.dependencies.black, 'black dep ignored');
t.notOk(pkg.dependencies.stdeb, 'stdeb dep ignored');

t.end();
.catch((error) => {
t.match(
normalize(error.message),
'No dependencies detected in manifest.'
);
});
});

Expand Down Expand Up @@ -1058,3 +1057,20 @@ test('package names with urls are skipped', (t) => {
);
});
});

test('inspect Pipfile with no deps or dev-deps exits with message', (t) => {
return Promise.resolve()
.then(() => {
chdirWorkspaces('pipfile-empty');
t.teardown(testUtils.activateVirtualenv('pip-app'));
})
.then(() => {
return plugin.inspect('.', 'Pipfile');
})
.catch((error) => {
t.match(
normalize(error.message),
'No dependencies detected in manifest.'
);
});
});
11 changes: 11 additions & 0 deletions test/workspaces/pipfile-empty/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]

[requires]
python_version = "2.7"

0 comments on commit 1aa6893

Please sign in to comment.