Skip to content

Commit

Permalink
Auto merge of #3253 - Turbo87:features-list, r=locks
Browse files Browse the repository at this point in the history
models/version: Add `features/featureList` properties

Each published `version` can have different optional `features` and it would be nice to see when a feature has been introduced. This PR prepares for showing this information in the versions list by adding `features` and `featureList` properties to the `version` model.
  • Loading branch information
bors committed Feb 8, 2021
2 parents 12b92d8 + a2592b8 commit 7e428dc
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app/models/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default class Version extends Model {
@attr('date') created_at;
@attr('date') updated_at;
@attr downloads;
@attr features;
@attr yanked;
@attr license;
@attr crate_size;
Expand All @@ -29,6 +30,19 @@ export default class Version extends Model {
})
crateName;

get featureList() {
let { features } = this;
if (typeof features !== 'object' || features === null) {
return [];
}

let defaultFeatures = features.default ?? [];
return Object.keys(features)
.filter(name => name !== 'default')
.sort()
.map(name => ({ name, isDefault: defaultFeatures.includes(name), dependencies: features[name] }));
}

@alias('loadAuthorsTask.last.value') authorNames;

@(task(function* () {
Expand Down
81 changes: 81 additions & 0 deletions tests/models/version-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,87 @@ module('Model | Version', function (hooks) {
this.store = this.owner.lookup('service:store');
});

module('featuresList', function () {
async function prepare(context, { features }) {
let { server, store } = context;

let crate = server.create('crate');
server.create('version', { crate, features });

let crateRecord = await store.findRecord('crate', crate.id);
let versions = (await crateRecord.versions).toArray();
return versions[0];
}

test('`features: {}` results in empty list', async function (assert) {
let version = await prepare(this, { features: {} });
assert.deepEqual(version.featureList, []);
});

test('`features: null` results in empty list', async function (assert) {
let version = await prepare(this, { features: null });
assert.deepEqual(version.featureList, []);
});

test('real world case', async function (assert) {
let features = {
alloc: ['rand_core/alloc'],
default: ['std', 'std_rng'],
getrandom: ['rand_core/getrandom'],
nightly: [],
serde1: ['serde'],
simd_support: ['packed_simd'],
small_rng: [],
std: ['rand_core/std', 'rand_chacha/std', 'alloc', 'getrandom', 'libc'],
std_rng: ['rand_chacha', 'rand_hc'],
};

let version = await prepare(this, { features });
assert.deepEqual(version.featureList, [
{
dependencies: ['rand_core/alloc'],
isDefault: false,
name: 'alloc',
},
{
dependencies: ['rand_core/getrandom'],
isDefault: false,
name: 'getrandom',
},
{
dependencies: [],
isDefault: false,
name: 'nightly',
},
{
dependencies: ['serde'],
isDefault: false,
name: 'serde1',
},
{
dependencies: ['packed_simd'],
isDefault: false,
name: 'simd_support',
},
{
dependencies: [],
isDefault: false,
name: 'small_rng',
},
{
dependencies: ['rand_core/std', 'rand_chacha/std', 'alloc', 'getrandom', 'libc'],
isDefault: true,
name: 'std',
},
{
dependencies: ['rand_chacha', 'rand_hc'],
isDefault: true,
name: 'std_rng',
},
]);
});
});

test('`published_by` relationship is assigned correctly', async function (assert) {
let user = this.server.create('user', { name: 'JD' });

Expand Down

0 comments on commit 7e428dc

Please sign in to comment.