Skip to content
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
9 changes: 9 additions & 0 deletions __fixtures__/docsPathsAllVersionsMock.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
{
"terraform-docs-common": {
"v0.0.x": [
{
"path": "terraform/plugin/debugging",
"itemPath": "content/terraform-docs-common/docs/plugin/debugging.mdx",
"created_at": "2025-06-03T18:02:21+00:00"
}
]
},
"terraform-plugin-framework": {
"v1.14.x": [
{
Expand Down
Binary file modified scripts/prebuild/prebuild-arm-linux-binary.gz
Binary file not shown.
Binary file modified scripts/prebuild/prebuild-arm-mac-binary.gz
Binary file not shown.
Binary file modified scripts/prebuild/prebuild-x64-linux-binary.gz
Binary file not shown.
34 changes: 29 additions & 5 deletions scripts/utils/file-path/url/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,38 @@ export function getUrlFromFilePath(
productConfig = PRODUCT_CONFIG,
) {
const repoDir = getProductDirectoryFromFilePath(filePath)
const version = getVersionFromFilePath(filePath)
const isValidProduct = productConfig[repoDir]

if (!isValidProduct) {
throw new Error(`Product not found for ${repoDir}`)
} else {
return allDocsPaths[repoDir][version].find((path) => {
return filePath.endsWith(path.itemPath)
}).path
}

// Check for versionless products and use v0.0.x as the version key
const version = productConfig[repoDir].versionedDocs
? getVersionFromFilePath(filePath)
: 'v0.0.x'

// Check if the version exists in allDocsPaths
if (!allDocsPaths[repoDir]) {
throw new Error(`No docs paths found for product: ${repoDir}`)
}

if (!allDocsPaths[repoDir][version]) {
throw new Error(
`Version ${version} not found for product ${repoDir}. File path: ${filePath}`,
)
}

// Find the matching path
const matchedPath = allDocsPaths[repoDir][version].find((path) => {
return filePath.endsWith(path.itemPath)
})

if (!matchedPath) {
throw new Error(
`No matching path found for file: ${filePath} in version ${version} of ${repoDir}`,
)
}

return matchedPath.path
}
101 changes: 101 additions & 0 deletions scripts/utils/file-path/url/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,105 @@ describe('getUrlFromFilePath', () => {
return getUrlFromFilePath(filePath, allDocsPathsJsonMock, PRODUCT_CONFIG)
}).toThrow(`Product not found for ${repoDir}`)
})

it('should use v0.0.x for versionless products (terraform-docs-common)', () => {
const filePath = 'content/terraform-docs-common/docs/plugin/debugging.mdx'
const repoDir = 'terraform-docs-common'
const expectedUrl = 'terraform/plugin/debugging'

getProductDirectoryFromFilePath.mockReturnValue(repoDir)
// versionedDocs: false, so should use v0.0.x

const result = getUrlFromFilePath(
filePath,
allDocsPathsJsonMock,
PRODUCT_CONFIG,
)
expect(result).toBe(expectedUrl)
})

it('should use v0.0.x for versionless products (hcp-docs)', () => {
const filePath =
'content/hcp-docs/content/docs/vagrant/reclaim-vagrant-cloud.mdx'
const repoDir = 'hcp-docs'
const expectedUrl = 'hcp/docs/vagrant/reclaim-vagrant-cloud'

// Add hcp-docs to product config for this test only
const mockProductConfig = {
...PRODUCT_CONFIG,
'hcp-docs': {
versionedDocs: false,
productSlug: 'hcp',
},
}

// Add hcp-docs to allDocsPaths for this test only
const mockAllDocsPaths = {
...allDocsPathsJsonMock,
'hcp-docs': {
'v0.0.x': [
{
path: expectedUrl,
itemPath: filePath,
created_at: '2025-06-03T18:02:21+00:00',
},
],
},
}

getProductDirectoryFromFilePath.mockReturnValue(repoDir)
// versionedDocs: false, so should use v0.0.x

const result = getUrlFromFilePath(
filePath,
mockAllDocsPaths,
mockProductConfig,
)
expect(result).toBe(expectedUrl)
})

it('should throw an error if no docs paths found for product', () => {
const filePath = 'content/terraform-plugin-framework/v1.14.x/docs/test.mdx'
const repoDir = 'terraform-plugin-framework'
const versionValue = 'v1.14.x'
const allDocsPathsEmpty = {}

getProductDirectoryFromFilePath.mockReturnValue(repoDir)
getVersionFromFilePath.mockReturnValue(versionValue)

expect(() => {
return getUrlFromFilePath(filePath, allDocsPathsEmpty, PRODUCT_CONFIG)
}).toThrow(`No docs paths found for product: ${repoDir}`)
})

it('should throw an error if version not found for product', () => {
const filePath = 'content/terraform-plugin-framework/v99.99.x/docs/test.mdx'
const repoDir = 'terraform-plugin-framework'
const versionValue = 'v99.99.x'

getProductDirectoryFromFilePath.mockReturnValue(repoDir)
getVersionFromFilePath.mockReturnValue(versionValue)

expect(() => {
return getUrlFromFilePath(filePath, allDocsPathsJsonMock, PRODUCT_CONFIG)
}).toThrow(
`Version ${versionValue} not found for product ${repoDir}. File path: ${filePath}`,
)
})

it('should throw an error if no matching path found for file', () => {
const filePath =
'content/terraform-plugin-framework/v1.14.x/docs/nonexistent-file.mdx'
const repoDir = 'terraform-plugin-framework'
const versionValue = 'v1.14.x'

getProductDirectoryFromFilePath.mockReturnValue(repoDir)
getVersionFromFilePath.mockReturnValue(versionValue)

expect(() => {
return getUrlFromFilePath(filePath, allDocsPathsJsonMock, PRODUCT_CONFIG)
}).toThrow(
`No matching path found for file: ${filePath} in version ${versionValue} of ${repoDir}`,
)
})
})
Loading