Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(versions/update): various edge cases #965

Merged
merged 5 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions __tests__/cmds/versions/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,34 @@ describe('rdme versions:update', () => {
delete process.env.TEST_RDME_CI;
});

it('should update a specific version object', async () => {
it('should update a specific version object using prompts', async () => {
const versionToChange = '1.1.0';
prompts.inject([versionToChange, undefined, false, true, false, false]);

const updatedVersionObject = {
version: versionToChange,
is_stable: false,
is_beta: true,
is_deprecated: false,
is_hidden: false,
};

const mockRequest = getAPIMock()
.get('/api/v1/version')
.basicAuth({ user: key })
.reply(200, [{ version }, { version: versionToChange }])
.get(`/api/v1/version/${versionToChange}`)
.basicAuth({ user: key })
.reply(200, { version: versionToChange })
.put(`/api/v1/version/${versionToChange}`, updatedVersionObject)
.basicAuth({ user: key })
.reply(201, updatedVersionObject);

await expect(updateVersion.run({ key })).resolves.toBe(`Version ${versionToChange} updated successfully.`);
mockRequest.done();
});

it('should rename a specific version object using prompts', async () => {
const versionToChange = '1.1.0';
const renamedVersion = '1.1.0-update';
prompts.inject([versionToChange, renamedVersion, false, true, false, false]);
Expand All @@ -54,7 +81,31 @@ describe('rdme versions:update', () => {
.reply(200, [{ version }, { version: versionToChange }])
.get(`/api/v1/version/${versionToChange}`)
.basicAuth({ user: key })
.reply(200, { version })
.reply(200, { version: versionToChange })
.put(`/api/v1/version/${versionToChange}`, updatedVersionObject)
.basicAuth({ user: key })
.reply(201, updatedVersionObject);

await expect(updateVersion.run({ key })).resolves.toBe(`Version ${versionToChange} updated successfully.`);
mockRequest.done();
});

it('should use subset of prompts when updating stable version', async () => {
const versionToChange = '1.1.0';
prompts.inject([versionToChange, undefined, true]);

const updatedVersionObject = {
version: versionToChange,
is_beta: true,
};

const mockRequest = getAPIMock()
.get('/api/v1/version')
.basicAuth({ user: key })
.reply(200, [{ version }, { version: versionToChange, is_stable: true }])
.get(`/api/v1/version/${versionToChange}`)
.basicAuth({ user: key })
.reply(200, { version: versionToChange, is_stable: true })
.put(`/api/v1/version/${versionToChange}`, updatedVersionObject)
.basicAuth({ user: key })
.reply(201, updatedVersionObject);
Expand Down
4 changes: 2 additions & 2 deletions src/cmds/versions/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default class UpdateVersionCommand extends Command {

// TODO: I think this fetch here is unnecessary but
// it will require a bigger refactor of getProjectVersion
const foundVersion = await readmeAPIFetch(`/api/v1/version/${selectedVersion}`, {
const foundVersion: Version = await readmeAPIFetch(`/api/v1/version/${selectedVersion}`, {
method: 'get',
headers: cleanHeaders(key),
}).then(handleRes);
Expand All @@ -66,7 +66,7 @@ export default class UpdateVersionCommand extends Command {
const body: Version = {
codename,
// fallback to existing version if user was prompted to rename the version but didn't enter anything
version: promptResponse.newVersion || version,
version: promptResponse.newVersion || selectedVersion,
is_beta: promptResponse.is_beta,
is_deprecated: promptResponse.is_deprecated,
is_hidden: promptResponse.is_hidden,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export function versionPrompt(
type: (prev, values) => {
// if user wants this version to be the main version
// it can't also be hidden.
return values.is_stable ? null : 'confirm';
return values.is_stable || existingVersion?.is_stable ? null : 'confirm';
},
name: 'is_hidden',
message: 'Should this version be hidden?',
Expand All @@ -193,7 +193,7 @@ export function versionPrompt(
type: (prev, values) => {
// if user wants this version to be the main version
// it can't also be deprecated.
return values.is_stable ? null : 'confirm';
return values.is_stable || existingVersion?.is_stable ? null : 'confirm';
},
name: 'is_deprecated',
message: 'Should this version be deprecated?',
Expand Down