test(upload): add unit tests for getRegistryTag function

- Introduced a new test suite for the getRegistryTag function, covering various scenarios including handling of usernames, image prefixes, and custom registry URLs.
- Ensured that the function correctly constructs image tags based on different input conditions, improving test coverage and reliability.
This commit is contained in:
Mauricio Siu
2025-12-09 23:54:54 -06:00
parent c233ddb520
commit ee411ac74f
2 changed files with 241 additions and 3 deletions

View File

@@ -74,11 +74,40 @@ export const uploadImageRemoteCommand = async (
throw error;
}
};
/**
* Extract the repository name from imageName by taking the last part after '/'
* Examples:
* - "nginx" -> "nginx"
* - "nginx:latest" -> "nginx:latest"
* - "myuser/myrepo" -> "myrepo"
* - "myuser/myrepo:tag" -> "myrepo:tag"
* - "docker.io/myuser/myrepo" -> "myrepo"
*/
const extractRepositoryName = (imageName: string): string => {
const lastSlashIndex = imageName.lastIndexOf("/");
// If no '/', return the imageName as is
if (lastSlashIndex === -1) {
return imageName;
}
// Extract everything after the last '/'
return imageName.substring(lastSlashIndex + 1);
};
export const getRegistryTag = (registry: Registry, imageName: string) => {
const { registryUrl, imagePrefix, username } = registry;
return imagePrefix
? `${registryUrl ? `${registryUrl}/` : ""}${imagePrefix}/${imageName}`
: `${registryUrl ? `${registryUrl}/` : ""}${username}/${imageName}`;
// Extract the repository name (last part after '/')
const repositoryName = extractRepositoryName(imageName);
// Build the final tag using registry's username/prefix
const targetPrefix = imagePrefix || username;
const finalRegistry = registryUrl || "";
return finalRegistry
? `${finalRegistry}/${targetPrefix}/${repositoryName}`
: `${targetPrefix}/${repositoryName}`;
};
const getRegistryCommands = (