mirror of
https://github.com/Azure/k8s-set-context.git
synced 2026-06-24 15:59:27 +08:00
Vidya reddy pretty code (#53)
* updated action file with node16 * Code consistency using prettier and its workflow * Enforce Prettier * code fix * code fix * code fix Co-authored-by: Vidya Reddy <vidyareddy@microsoft.com>
This commit is contained in:
+20
-20
@@ -1,24 +1,24 @@
|
||||
import { Cluster, parseCluster } from "./cluster";
|
||||
import {Cluster, parseCluster} from './cluster'
|
||||
|
||||
describe("Cluster type", () => {
|
||||
test("it has required values", () => {
|
||||
const vals = <any>Object.values(Cluster);
|
||||
expect(vals.includes("arc")).toBe(true);
|
||||
expect(vals.includes("generic")).toBe(true);
|
||||
});
|
||||
describe('Cluster type', () => {
|
||||
test('it has required values', () => {
|
||||
const vals = <any>Object.values(Cluster)
|
||||
expect(vals.includes('arc')).toBe(true)
|
||||
expect(vals.includes('generic')).toBe(true)
|
||||
})
|
||||
|
||||
test("it can parse valid values from a string", () => {
|
||||
expect(parseCluster("arc")).toBe(Cluster.ARC);
|
||||
expect(parseCluster("Arc")).toBe(Cluster.ARC);
|
||||
expect(parseCluster("ARC")).toBe(Cluster.ARC);
|
||||
test('it can parse valid values from a string', () => {
|
||||
expect(parseCluster('arc')).toBe(Cluster.ARC)
|
||||
expect(parseCluster('Arc')).toBe(Cluster.ARC)
|
||||
expect(parseCluster('ARC')).toBe(Cluster.ARC)
|
||||
|
||||
expect(parseCluster("generic")).toBe(Cluster.GENERIC);
|
||||
expect(parseCluster("Generic")).toBe(Cluster.GENERIC);
|
||||
expect(parseCluster("GENERIC")).toBe(Cluster.GENERIC);
|
||||
});
|
||||
expect(parseCluster('generic')).toBe(Cluster.GENERIC)
|
||||
expect(parseCluster('Generic')).toBe(Cluster.GENERIC)
|
||||
expect(parseCluster('GENERIC')).toBe(Cluster.GENERIC)
|
||||
})
|
||||
|
||||
test("it will return undefined if it can't parse values from a string", () => {
|
||||
expect(parseCluster("invalid")).toBe(undefined);
|
||||
expect(parseCluster("unsupportedType")).toBe(undefined);
|
||||
});
|
||||
});
|
||||
test("it will return undefined if it can't parse values from a string", () => {
|
||||
expect(parseCluster('invalid')).toBe(undefined)
|
||||
expect(parseCluster('unsupportedType')).toBe(undefined)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
export enum Cluster {
|
||||
ARC = "arc",
|
||||
GENERIC = "generic",
|
||||
ARC = 'arc',
|
||||
GENERIC = 'generic'
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -9,8 +9,8 @@ export enum Cluster {
|
||||
* @returns The Cluster enum or undefined if it can't be parsed
|
||||
*/
|
||||
export const parseCluster = (str: string): Cluster | undefined =>
|
||||
Cluster[
|
||||
Object.keys(Cluster).filter(
|
||||
(k) => Cluster[k].toString().toLowerCase() === str.toLowerCase()
|
||||
)[0] as keyof typeof Cluster
|
||||
];
|
||||
Cluster[
|
||||
Object.keys(Cluster).filter(
|
||||
(k) => Cluster[k].toString().toLowerCase() === str.toLowerCase()
|
||||
)[0] as keyof typeof Cluster
|
||||
]
|
||||
|
||||
+28
-28
@@ -1,33 +1,33 @@
|
||||
import { parseK8sSecret, K8sSecret } from "./k8sSecret";
|
||||
import {parseK8sSecret, K8sSecret} from './k8sSecret'
|
||||
|
||||
describe("K8sSecret type", () => {
|
||||
describe("Parsing from any", () => {
|
||||
test("it returns a type guarded secret", () => {
|
||||
const secret = { data: { token: "token", "ca.crt": "cert" } };
|
||||
expect(() => parseK8sSecret(secret)).not.toThrow();
|
||||
});
|
||||
describe('K8sSecret type', () => {
|
||||
describe('Parsing from any', () => {
|
||||
test('it returns a type guarded secret', () => {
|
||||
const secret = {data: {token: 'token', 'ca.crt': 'cert'}}
|
||||
expect(() => parseK8sSecret(secret)).not.toThrow()
|
||||
})
|
||||
|
||||
test("it throws an error when secret not provided", () => {
|
||||
expect(() => parseK8sSecret(undefined)).toThrow();
|
||||
});
|
||||
test('it throws an error when secret not provided', () => {
|
||||
expect(() => parseK8sSecret(undefined)).toThrow()
|
||||
})
|
||||
|
||||
test("it throws an error when there is no data field", () => {
|
||||
const secret = {};
|
||||
expect(() => parseK8sSecret(secret)).toThrow();
|
||||
});
|
||||
test('it throws an error when there is no data field', () => {
|
||||
const secret = {}
|
||||
expect(() => parseK8sSecret(secret)).toThrow()
|
||||
})
|
||||
|
||||
test("it throws an error when there is no token", () => {
|
||||
const secret = {
|
||||
data: {
|
||||
"ca.crt": "cert",
|
||||
},
|
||||
};
|
||||
expect(() => parseK8sSecret(secret)).toThrow();
|
||||
});
|
||||
test('it throws an error when there is no token', () => {
|
||||
const secret = {
|
||||
data: {
|
||||
'ca.crt': 'cert'
|
||||
}
|
||||
}
|
||||
expect(() => parseK8sSecret(secret)).toThrow()
|
||||
})
|
||||
|
||||
test("it throws an error when there is no ca.crt field", () => {
|
||||
const secret = { data: { token: "token" } };
|
||||
expect(() => parseK8sSecret(secret)).toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
test('it throws an error when there is no ca.crt field', () => {
|
||||
const secret = {data: {token: 'token'}}
|
||||
expect(() => parseK8sSecret(secret)).toThrow()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
+11
-11
@@ -1,10 +1,10 @@
|
||||
import * as util from "util";
|
||||
import * as util from 'util'
|
||||
|
||||
export interface K8sSecret {
|
||||
data: {
|
||||
token: string;
|
||||
"ca.crt": string;
|
||||
};
|
||||
data: {
|
||||
token: string
|
||||
'ca.crt': string
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -13,13 +13,13 @@ export interface K8sSecret {
|
||||
* @returns A type guarded K8sSecret
|
||||
*/
|
||||
export function parseK8sSecret(secret: any): K8sSecret {
|
||||
if (!secret) throw Error("K8s secret yaml is invalid");
|
||||
if (!secret.data) throw k8sSecretMissingFieldError("data");
|
||||
if (!secret.data.token) throw k8sSecretMissingFieldError("token");
|
||||
if (!secret.data["ca.crt"]) throw k8sSecretMissingFieldError("ca.crt");
|
||||
if (!secret) throw Error('K8s secret yaml is invalid')
|
||||
if (!secret.data) throw k8sSecretMissingFieldError('data')
|
||||
if (!secret.data.token) throw k8sSecretMissingFieldError('token')
|
||||
if (!secret.data['ca.crt']) throw k8sSecretMissingFieldError('ca.crt')
|
||||
|
||||
return secret as K8sSecret;
|
||||
return secret as K8sSecret
|
||||
}
|
||||
|
||||
const k8sSecretMissingFieldError = (field: string): Error =>
|
||||
Error(util.format("K8s secret yaml does not contain %s field", field));
|
||||
Error(util.format('K8s secret yaml does not contain %s field', field))
|
||||
|
||||
+24
-24
@@ -1,29 +1,29 @@
|
||||
import { Method, parseMethod } from "./method";
|
||||
import {Method, parseMethod} from './method'
|
||||
|
||||
describe("Method type", () => {
|
||||
test("it has required values", () => {
|
||||
const vals = <any>Object.values(Method);
|
||||
expect(vals.includes("kubeconfig")).toBe(true);
|
||||
expect(vals.includes("service-account")).toBe(true);
|
||||
expect(vals.includes("service-principal")).toBe(true);
|
||||
});
|
||||
describe('Method type', () => {
|
||||
test('it has required values', () => {
|
||||
const vals = <any>Object.values(Method)
|
||||
expect(vals.includes('kubeconfig')).toBe(true)
|
||||
expect(vals.includes('service-account')).toBe(true)
|
||||
expect(vals.includes('service-principal')).toBe(true)
|
||||
})
|
||||
|
||||
test("it can parse valid values from a string", () => {
|
||||
expect(parseMethod("kubeconfig")).toBe(Method.KUBECONFIG);
|
||||
expect(parseMethod("Kubeconfig")).toBe(Method.KUBECONFIG);
|
||||
expect(parseMethod("KUBECONFIG")).toBe(Method.KUBECONFIG);
|
||||
test('it can parse valid values from a string', () => {
|
||||
expect(parseMethod('kubeconfig')).toBe(Method.KUBECONFIG)
|
||||
expect(parseMethod('Kubeconfig')).toBe(Method.KUBECONFIG)
|
||||
expect(parseMethod('KUBECONFIG')).toBe(Method.KUBECONFIG)
|
||||
|
||||
expect(parseMethod("service-account")).toBe(Method.SERVICE_ACCOUNT);
|
||||
expect(parseMethod("Service-Account")).toBe(Method.SERVICE_ACCOUNT);
|
||||
expect(parseMethod("SERVICE-ACCOUNT")).toBe(Method.SERVICE_ACCOUNT);
|
||||
expect(parseMethod('service-account')).toBe(Method.SERVICE_ACCOUNT)
|
||||
expect(parseMethod('Service-Account')).toBe(Method.SERVICE_ACCOUNT)
|
||||
expect(parseMethod('SERVICE-ACCOUNT')).toBe(Method.SERVICE_ACCOUNT)
|
||||
|
||||
expect(parseMethod("service-principal")).toBe(Method.SERVICE_PRINCIPAL);
|
||||
expect(parseMethod("Service-Principal")).toBe(Method.SERVICE_PRINCIPAL);
|
||||
expect(parseMethod("SERVICE-PRINCIPAL")).toBe(Method.SERVICE_PRINCIPAL);
|
||||
});
|
||||
expect(parseMethod('service-principal')).toBe(Method.SERVICE_PRINCIPAL)
|
||||
expect(parseMethod('Service-Principal')).toBe(Method.SERVICE_PRINCIPAL)
|
||||
expect(parseMethod('SERVICE-PRINCIPAL')).toBe(Method.SERVICE_PRINCIPAL)
|
||||
})
|
||||
|
||||
test("it will return undefined if it can't parse values from a string", () => {
|
||||
expect(parseMethod("invalid")).toBe(undefined);
|
||||
expect(parseMethod("unsupportedType")).toBe(undefined);
|
||||
});
|
||||
});
|
||||
test("it will return undefined if it can't parse values from a string", () => {
|
||||
expect(parseMethod('invalid')).toBe(undefined)
|
||||
expect(parseMethod('unsupportedType')).toBe(undefined)
|
||||
})
|
||||
})
|
||||
|
||||
+8
-8
@@ -1,7 +1,7 @@
|
||||
export enum Method {
|
||||
KUBECONFIG = "kubeconfig",
|
||||
SERVICE_ACCOUNT = "service-account",
|
||||
SERVICE_PRINCIPAL = "service-principal",
|
||||
KUBECONFIG = 'kubeconfig',
|
||||
SERVICE_ACCOUNT = 'service-account',
|
||||
SERVICE_PRINCIPAL = 'service-principal'
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -10,8 +10,8 @@ export enum Method {
|
||||
* @returns The Method enum or undefined if it can't be parsed
|
||||
*/
|
||||
export const parseMethod = (str: string): Method | undefined =>
|
||||
Method[
|
||||
Object.keys(Method).filter(
|
||||
(k) => Method[k].toString().toLowerCase() === str.toLowerCase()
|
||||
)[0] as keyof typeof Method
|
||||
];
|
||||
Method[
|
||||
Object.keys(Method).filter(
|
||||
(k) => Method[k].toString().toLowerCase() === str.toLowerCase()
|
||||
)[0] as keyof typeof Method
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user