Skip to content

Commit d9a96c5

Browse files
committed
Add extension API test
1 parent e8c61c7 commit d9a96c5

File tree

6 files changed

+36
-58
lines changed

6 files changed

+36
-58
lines changed

cli/src/commands/extensions/api.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::cell::RefCell;
2+
use std::fs;
23
use std::ops::Deref;
34
use std::path::Path;
45
use std::pin::Pin;
@@ -17,9 +18,10 @@ use phylum_types::types::package::{
1718
};
1819
use phylum_types::types::project::ProjectDetailsResponse;
1920

21+
use crate::api::PhylumApi;
22+
use crate::auth::UserInfo;
2023
use crate::commands::parse::{get_packages_from_lockfile, LOCKFILE_PARSERS};
2124
use crate::config::get_current_project;
22-
use crate::{api::PhylumApi, auth::UserInfo};
2325

2426
/// Holds either an unawaited, boxed `Future`, or the result of awaiting the future.
2527
enum OnceFuture<T: Unpin> {
@@ -233,13 +235,14 @@ async fn get_package_details(
233235
/// Parse a lockfile and return the package descriptors contained therein.
234236
/// Equivalent to `phylum parse`.
235237
#[op]
236-
fn parse_lockfile(lockfile: &str, lockfile_type: &str) -> Result<Vec<PackageDescriptor>> {
238+
fn parse_lockfile(lockfile: String, lockfile_type: String) -> Result<Vec<PackageDescriptor>> {
237239
let parser = LOCKFILE_PARSERS
238240
.iter()
239241
.find_map(|(name, parser)| (*name == lockfile_type).then(|| *parser))
240242
.ok_or_else(|| anyhow!("Unrecognized lockfile type: `{lockfile_type}`"))?;
241243

242-
let lockfile_data = std::fs::read_to_string(Path::new(lockfile))?;
244+
let lockfile_data = fs::read_to_string(&lockfile)
245+
.with_context(|| format!("Could not read lockfile at '{lockfile}'"))?;
243246
parser.parse(&lockfile_data)
244247
}
245248

cli/src/extension_api.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,9 @@ export class PhylumApi {
4444
static async getPackageDetails(name: string, version: string, packageType: string): object {
4545
return await Deno.core.opAsync('get_package_details', name, version, packageType);
4646
}
47+
48+
/// Get dependencies inside a lockfile.
49+
static parseLockfile(lockfile: string, lockfileType: string): [object] {
50+
return Deno.core.opSync('parse_lockfile', lockfile, lockfileType);
51+
}
4752
}

cli/tests/extensions.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,27 @@ fn extension_list_should_emit_output() {
225225
assert!(output.lines().any(|m| re.is_match(m)));
226226
}
227227

228+
// Extensions relying on the injected Phylum API work.
229+
#[tokio::test]
230+
async fn injected_api() {
231+
let tempdir = TempDir::new().unwrap();
232+
Command::cargo_bin("phylum")
233+
.unwrap()
234+
.env("XDG_DATA_HOME", tempdir.path())
235+
.args(&["extension", "add"])
236+
.arg(fixtures_path().join("api-extension"))
237+
.assert()
238+
.success();
239+
240+
Command::cargo_bin("phylum")
241+
.unwrap()
242+
.env("XDG_DATA_HOME", tempdir.path())
243+
.arg("api-extension")
244+
.assert()
245+
.success()
246+
.stdout("44\n");
247+
}
248+
228249
////////////////////////////////////////////////////////////////////////////////
229250
// Miscellaneous tests
230251
////////////////////////////////////////////////////////////////////////////////
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
name = "api-extension"
2-
description = "This extension tests the API ops"
2+
description = "Test phylum API injection"
33
entry_point = "main.ts"
Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,4 @@
1-
import * as PhylumApi from './phylum-api.ts'
1+
import { PhylumApi } from "phylum";
22

3-
// Extension tests should be ran against an integration context, and not in CI
4-
// at least for the moment, as they require access to an API instance, or a
5-
// well-formed, external mock of it.
6-
7-
// Deno.core.print(JSON.stringify(await PhylumApi.get_user_info()))
8-
// await PhylumApi.get_access_token()
9-
// await PhylumApi.get_refresh_token()
10-
// try {
11-
// await PhylumApi.get_job_status()
12-
// } catch(e) {
13-
// Deno.core.print(JSON.stringify(e))
14-
// }
15-
// try {
16-
// await PhylumApi.get_project_details()
17-
// } catch(e) {
18-
// Deno.core.print(JSON.stringify(e))
19-
// }
3+
const packages = PhylumApi.parseLockfile("./tests/fixtures/poetry.lock", "poetry");
4+
console.log(packages.length);

cli/tests/fixtures/extensions/api-extension/phylum-api.ts

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)