Skip to content

Commit 99a7805

Browse files
committed
✨ Support workspace Cargo.toml with package section
This commit also introduce: - βœ… Unit test for previous behavior - πŸ› make description optional field like in other package
1 parent d0edd29 commit 99a7805

File tree

2 files changed

+101
-11
lines changed

2 files changed

+101
-11
lines changed

β€Žpackages/gitmoji-changelog-cli/src/presets/cargo.jsβ€Ž

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
const toml = require('toml')
22
const fs = require('fs')
33

4+
function configFromPackage(tomlEntry) {
5+
let config = {
6+
name: tomlEntry.name,
7+
version: tomlEntry.version,
8+
description: tomlEntry.description,
9+
}
10+
11+
if (!config.name) {
12+
throw new Error('Could not find name metadata in Cargo.toml')
13+
}
14+
if (!config.version) {
15+
throw new Error('Could not find version metadata in Cargo.toml')
16+
}
17+
if (!config.description) {
18+
config.description = ''
19+
}
20+
21+
return config
22+
}
23+
424
module.exports = async () => {
525
try {
626
const cargoPromise = new Promise((resolve, reject) => {
@@ -11,18 +31,22 @@ module.exports = async () => {
1131
}
1232
})
1333

14-
const {
15-
package: {
16-
name,
17-
version,
18-
description,
19-
},
20-
} = await cargoPromise
21-
return {
22-
name: name,
23-
version: version,
24-
description: description,
34+
let cargoToml = await cargoPromise
35+
if (!cargoToml) {
36+
throw new Error('Cargo.toml is empty')
37+
}
38+
39+
// Check if the package section exists, it means it's a Cargo.toml file
40+
if (cargoToml.package) {
41+
return configFromPackage(cargoToml.package)
42+
} if (cargoToml.workspace) {
43+
const workspace = cargoToml.workspace
44+
if (!workspace.package) {
45+
throw new Error("Cargo.toml workspace doesn't have a package section")
46+
}
47+
return configFromPackage(workspace.package)
2548
}
49+
throw Error("Cargo.toml doesn't have a package or workspace section")
2650
} catch (e) {
2751
return null
2852
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const fs = require('fs')
2+
3+
const loadProjectInfo = require('./cargo.js')
4+
5+
describe('getPackageInfo', () => {
6+
it('should extract metadata from a Cargo.toml that is a crate', async () => {
7+
// Reference content from https://doc.rust-lang.org/cargo/reference/manifest.html#the-package-section
8+
fs.readFileSync.mockReturnValue(`
9+
[package]
10+
name = "hello_world" # the name of the package
11+
version = "0.1.0" # the current version, obeying semver
12+
authors = ["Alice <[email protected]>", "Bob <[email protected]>"]
13+
description = "A sample project"
14+
`)
15+
16+
const result = await loadProjectInfo()
17+
18+
expect(result).toEqual({
19+
name: 'hello_world',
20+
version: '0.1.0',
21+
description: 'A sample project',
22+
})
23+
})
24+
25+
it('should extract metadata from a Cargo.toml with missing description', async () => {
26+
fs.readFileSync.mockReturnValue(`
27+
[package]
28+
name = "no-description" # the name of the package
29+
version = "0.0.1" # the current version, obeying semver
30+
`)
31+
32+
const result = await loadProjectInfo()
33+
34+
expect(result).toEqual({
35+
name: 'no-description',
36+
version: '0.0.1',
37+
description: '',
38+
})
39+
})
40+
41+
it('should extract metadata from a Cargo.toml that is a workspace', async () => {
42+
// Reference content from https://doc.rust-lang.org/cargo/reference/workspaces.html#the-package-table
43+
fs.readFileSync.mockReturnValue(`
44+
[workspace]
45+
members = ["bar"]
46+
47+
[workspace.package]
48+
name = "hello_world"
49+
version = "1.2.3"
50+
authors = ["Nice Folks"]
51+
description = "A short description of my package"
52+
documentation = "https://example.com/bar"
53+
`)
54+
55+
const result = await loadProjectInfo()
56+
57+
expect(result).toEqual({
58+
name: 'hello_world',
59+
version: '1.2.3',
60+
description: 'A short description of my package',
61+
})
62+
})
63+
})
64+
65+
66+
jest.mock('fs')

0 commit comments

Comments
Β (0)