test(cli/clitest): pad replacements for table alignment (#15942)

This commit is contained in:
Mathias Fredriksson
2024-12-20 00:06:22 +02:00
committed by GitHub
parent 63572d9f53
commit d59fddbf6a
4 changed files with 61 additions and 51 deletions

View File

@ -128,7 +128,7 @@ func TestGoldenFile(t *testing.T, fileName string, actual []byte, replacements m
// equality check.
func normalizeGoldenFile(t *testing.T, byt []byte) []byte {
// Replace any timestamps with a placeholder.
byt = timestampRegex.ReplaceAll(byt, []byte("[timestamp]"))
byt = timestampRegex.ReplaceAll(byt, []byte(pad("[timestamp]", 20)))
homeDir, err := os.UserHomeDir()
require.NoError(t, err)
@ -202,21 +202,31 @@ func prepareTestData(t *testing.T) (*codersdk.Client, map[string]string) {
workspaceBuild := coderdtest.AwaitWorkspaceBuildJobCompleted(t, rootClient, workspace.LatestBuild.ID)
replacements := map[string]string{
firstUser.UserID.String(): "[first user ID]",
secondUser.ID.String(): "[second user ID]",
firstUser.OrganizationID.String(): "[first org ID]",
version.ID.String(): "[version ID]",
version.Name: "[version name]",
version.Job.ID.String(): "[version job ID]",
version.Job.FileID.String(): "[version file ID]",
version.Job.WorkerID.String(): "[version worker ID]",
template.ID.String(): "[template ID]",
workspace.ID.String(): "[workspace ID]",
workspaceBuild.ID.String(): "[workspace build ID]",
workspaceBuild.Job.ID.String(): "[workspace build job ID]",
workspaceBuild.Job.FileID.String(): "[workspace build file ID]",
workspaceBuild.Job.WorkerID.String(): "[workspace build worker ID]",
firstUser.UserID.String(): pad("[first user ID]", 36),
secondUser.ID.String(): pad("[second user ID]", 36),
firstUser.OrganizationID.String(): pad("[first org ID]", 36),
version.ID.String(): pad("[version ID]", 36),
version.Name: pad("[version name]", 36),
version.Job.ID.String(): pad("[version job ID]", 36),
version.Job.FileID.String(): pad("[version file ID]", 36),
version.Job.WorkerID.String(): pad("[version worker ID]", 36),
template.ID.String(): pad("[template ID]", 36),
workspace.ID.String(): pad("[workspace ID]", 36),
workspaceBuild.ID.String(): pad("[workspace build ID]", 36),
workspaceBuild.Job.ID.String(): pad("[workspace build job ID]", 36),
workspaceBuild.Job.FileID.String(): pad("[workspace build file ID]", 36),
workspaceBuild.Job.WorkerID.String(): pad("[workspace build worker ID]", 36),
}
return rootClient, replacements
}
func pad(s string, n int) string {
if len(s) >= n {
return s
}
n -= len(s)
pre := n / 2
post := n - pre
return strings.Repeat("=", pre) + s + strings.Repeat("=", post)
}

View File

@ -1,43 +1,43 @@
[
{
"id": "[workspace ID]",
"created_at": "[timestamp]",
"updated_at": "[timestamp]",
"owner_id": "[first user ID]",
"id": "===========[workspace ID]===========",
"created_at": "====[timestamp]=====",
"updated_at": "====[timestamp]=====",
"owner_id": "==========[first user ID]===========",
"owner_name": "testuser",
"owner_avatar_url": "",
"organization_id": "[first org ID]",
"organization_id": "===========[first org ID]===========",
"organization_name": "coder",
"template_id": "[template ID]",
"template_id": "===========[template ID]============",
"template_name": "test-template",
"template_display_name": "",
"template_icon": "",
"template_allow_user_cancel_workspace_jobs": false,
"template_active_version_id": "[version ID]",
"template_active_version_id": "============[version ID]============",
"template_require_active_version": false,
"latest_build": {
"id": "[workspace build ID]",
"created_at": "[timestamp]",
"updated_at": "[timestamp]",
"workspace_id": "[workspace ID]",
"id": "========[workspace build ID]========",
"created_at": "====[timestamp]=====",
"updated_at": "====[timestamp]=====",
"workspace_id": "===========[workspace ID]===========",
"workspace_name": "test-workspace",
"workspace_owner_id": "[first user ID]",
"workspace_owner_id": "==========[first user ID]===========",
"workspace_owner_name": "testuser",
"workspace_owner_avatar_url": "",
"template_version_id": "[version ID]",
"template_version_name": "[version name]",
"template_version_id": "============[version ID]============",
"template_version_name": "===========[version name]===========",
"build_number": 1,
"transition": "start",
"initiator_id": "[first user ID]",
"initiator_id": "==========[first user ID]===========",
"initiator_name": "testuser",
"job": {
"id": "[workspace build job ID]",
"created_at": "[timestamp]",
"started_at": "[timestamp]",
"completed_at": "[timestamp]",
"id": "======[workspace build job ID]======",
"created_at": "====[timestamp]=====",
"started_at": "====[timestamp]=====",
"completed_at": "====[timestamp]=====",
"status": "succeeded",
"worker_id": "[workspace build worker ID]",
"file_id": "[workspace build file ID]",
"worker_id": "====[workspace build worker ID]=====",
"file_id": "=====[workspace build file ID]======",
"tags": {
"owner": "",
"scope": "organization"
@ -47,7 +47,7 @@
},
"reason": "initiator",
"resources": [],
"deadline": "[timestamp]",
"deadline": "====[timestamp]=====",
"max_deadline": null,
"status": "running",
"daily_cost": 0,
@ -61,7 +61,7 @@
"name": "test-workspace",
"autostart_schedule": "CRON_TZ=US/Central 30 9 * * 1-5",
"ttl_ms": 28800000,
"last_used_at": "[timestamp]",
"last_used_at": "====[timestamp]=====",
"deleting_at": null,
"dormant_at": null,
"health": {
@ -71,6 +71,6 @@
"automatic_updates": "never",
"allow_renames": false,
"favorite": false,
"next_start_at": "[timestamp]"
"next_start_at": "====[timestamp]====="
}
]

View File

@ -1,3 +1,3 @@
USERNAME EMAIL CREATED AT STATUS
testuser testuser@coder.com [timestamp] active
testuser2 testuser2@coder.com [timestamp] dormant
testuser testuser@coder.com ====[timestamp]===== active
testuser2 testuser2@coder.com ====[timestamp]===== dormant

View File

@ -1,18 +1,18 @@
[
{
"id": "[first user ID]",
"id": "==========[first user ID]===========",
"username": "testuser",
"avatar_url": "",
"name": "Test User",
"email": "testuser@coder.com",
"created_at": "[timestamp]",
"updated_at": "[timestamp]",
"last_seen_at": "[timestamp]",
"created_at": "====[timestamp]=====",
"updated_at": "====[timestamp]=====",
"last_seen_at": "====[timestamp]=====",
"status": "active",
"login_type": "password",
"theme_preference": "",
"organization_ids": [
"[first org ID]"
"===========[first org ID]==========="
],
"roles": [
{
@ -22,19 +22,19 @@
]
},
{
"id": "[second user ID]",
"id": "==========[second user ID]==========",
"username": "testuser2",
"avatar_url": "",
"name": "",
"email": "testuser2@coder.com",
"created_at": "[timestamp]",
"updated_at": "[timestamp]",
"last_seen_at": "[timestamp]",
"created_at": "====[timestamp]=====",
"updated_at": "====[timestamp]=====",
"last_seen_at": "====[timestamp]=====",
"status": "dormant",
"login_type": "password",
"theme_preference": "",
"organization_ids": [
"[first org ID]"
"===========[first org ID]==========="
],
"roles": []
}