Skip to content

Commit

Permalink
feat: Add ability for the current version to be generated from the gi…
Browse files Browse the repository at this point in the history
…t sha
  • Loading branch information
TimothyJones committed Nov 29, 2024
1 parent d883140 commit dd70365
Show file tree
Hide file tree
Showing 40 changed files with 502 additions and 109 deletions.
12 changes: 9 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 24 additions & 9 deletions packages/case-connector-proto/proto/contract_case_stream.proto
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@ message ContractCaseConfig {
15; // Long term, this will be moved

map<string, string> mock_config = 16;

google.protobuf.StringValue auto_version_from = 17;
}

// Indicates a successful response with no payload
message ResultSuccess {}
message ResultSuccess {
}
message ResultSuccessHasMapPayload {
// Always a map of Strings -> Strings
google.protobuf.Struct map = 1;
Expand Down Expand Up @@ -74,7 +77,9 @@ message StateHandlerHandle {
}

// A reference to a trigger function that can be invoked
message TriggerFunctionHandle { google.protobuf.StringValue handle = 1; }
message TriggerFunctionHandle {
google.protobuf.StringValue handle = 1;
}

// Requests from client / host

Expand All @@ -100,11 +105,14 @@ message RunRejectingExampleRequest {

// From host to core, instructs the core to strip the matchers from a given
// matcher / data object
message StripMatchersRequest { google.protobuf.Struct matcher_or_data = 2; }
message StripMatchersRequest {
google.protobuf.Struct matcher_or_data = 2;
}

// From host to core, instructs the core to finish the current contract
// definition
message EndDefinitionRequest {}
message EndDefinitionRequest {
}

// From host to core, instructs the core to load a plugin
message LoadPluginRequest {
Expand All @@ -116,7 +124,9 @@ message LoadPluginRequest {
// Responses from server

// From Core to Host, instructs the host to run a given state handler
message RunStateHandlerRequest { StateHandlerHandle state_handler_handle = 1; }
message RunStateHandlerRequest {
StateHandlerHandle state_handler_handle = 1;
}

// From Core to Host, requests the host invoke one of the user-provided
// triggers.
Expand All @@ -129,12 +139,14 @@ message TriggerFunctionRequest {
SetupInfo setup = 3;
}

message CoreFunctionHandle { google.protobuf.StringValue handle = 1; }
message CoreFunctionHandle {
google.protobuf.StringValue handle = 1;
}

// Describes the setup of the currently executing Interaction
message SetupInfo {
// The resolved values for the state variables
map<string, google.protobuf.StringValue> stateVariables = 1;
map<string, google.protobuf.StringValue> state_variables = 1;
// The values of any mock setup information
map<string, google.protobuf.StringValue> mock = 2;
// Callbacks handles for functions
Expand Down Expand Up @@ -276,7 +288,9 @@ message PrintTestTitleRequest {
//
// Most messages must be acknowledged with a response - this indicates the
// return of the previous message.
message ResultResponse { BoundaryResult result = 1; }
message ResultResponse {
BoundaryResult result = 1;
}

// From the Host to the Core, requests that a verification session begins
message BeginVerificationRequest {
Expand All @@ -287,7 +301,8 @@ message BeginVerificationRequest {
}

// From the Host to the Core, requests a list of the available contracts
message AvailableContractDefinitions {}
message AvailableContractDefinitions {
}

// From the Host to the Core, instructs the core that the current verification
// session is now configured, and asks it to execute the verification.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ export class ContractCaseConfig extends jspb.Message {
getMockConfigMap(): jspb.Map<string, string>;
clearMockConfigMap(): void;

hasAutoVersionFrom(): boolean;
clearAutoVersionFrom(): void;
getAutoVersionFrom(): google_protobuf_wrappers_pb.StringValue | undefined;
setAutoVersionFrom(
value?: google_protobuf_wrappers_pb.StringValue,
): ContractCaseConfig;

serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): ContractCaseConfig.AsObject;
static toObject(
Expand Down Expand Up @@ -152,6 +159,7 @@ export namespace ContractCaseConfig {
baseUrlUnderTest?: google_protobuf_wrappers_pb.StringValue.AsObject;

mockConfigMap: Array<[string, string]>;
autoVersionFrom?: google_protobuf_wrappers_pb.StringValue.AsObject;
};

export class UsernamePassword extends jspb.Message {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,8 @@ proto.io.contract_testing.contractcase.grpc.ContractCaseConfig.toObject = functi
triggerAndTestsMap: (f = msg.getTriggerAndTestsMap()) ? f.toObject(includeInstance, proto.io.contract_testing.contractcase.grpc.TriggerFunctionHandle.toObject) : [],
triggerAndTest: (f = msg.getTriggerAndTest()) && proto.io.contract_testing.contractcase.grpc.TriggerFunctionHandle.toObject(includeInstance, f),
baseUrlUnderTest: (f = msg.getBaseUrlUnderTest()) && google_protobuf_wrappers_pb.StringValue.toObject(includeInstance, f),
mockConfigMap: (f = msg.getMockConfigMap()) ? f.toObject(includeInstance, undefined) : []
mockConfigMap: (f = msg.getMockConfigMap()) ? f.toObject(includeInstance, undefined) : [],
autoVersionFrom: (f = msg.getAutoVersionFrom()) && google_protobuf_wrappers_pb.StringValue.toObject(includeInstance, f)
};

if (includeInstance) {
Expand Down Expand Up @@ -952,6 +953,11 @@ proto.io.contract_testing.contractcase.grpc.ContractCaseConfig.deserializeBinary
jspb.Map.deserializeBinary(message, reader, jspb.BinaryReader.prototype.readString, jspb.BinaryReader.prototype.readString, null, "", "");
});
break;
case 17:
var value = new google_protobuf_wrappers_pb.StringValue;
reader.readMessage(value,google_protobuf_wrappers_pb.StringValue.deserializeBinaryFromReader);
msg.setAutoVersionFrom(value);
break;
default:
reader.skipField();
break;
Expand Down Expand Up @@ -1101,6 +1107,14 @@ proto.io.contract_testing.contractcase.grpc.ContractCaseConfig.serializeBinaryTo
if (f && f.getLength() > 0) {
f.serializeBinary(16, writer, jspb.BinaryWriter.prototype.writeString, jspb.BinaryWriter.prototype.writeString);
}
f = message.getAutoVersionFrom();
if (f != null) {
writer.writeMessage(
17,
f,
google_protobuf_wrappers_pb.StringValue.serializeBinaryToWriter
);
}
};


Expand Down Expand Up @@ -1869,6 +1883,43 @@ proto.io.contract_testing.contractcase.grpc.ContractCaseConfig.prototype.clearMo
return this;};


/**
* optional google.protobuf.StringValue auto_version_from = 17;
* @return {?proto.google.protobuf.StringValue}
*/
proto.io.contract_testing.contractcase.grpc.ContractCaseConfig.prototype.getAutoVersionFrom = function() {
return /** @type{?proto.google.protobuf.StringValue} */ (
jspb.Message.getWrapperField(this, google_protobuf_wrappers_pb.StringValue, 17));
};


/**
* @param {?proto.google.protobuf.StringValue|undefined} value
* @return {!proto.io.contract_testing.contractcase.grpc.ContractCaseConfig} returns this
*/
proto.io.contract_testing.contractcase.grpc.ContractCaseConfig.prototype.setAutoVersionFrom = function(value) {
return jspb.Message.setWrapperField(this, 17, value);
};


/**
* Clears the message field making it undefined.
* @return {!proto.io.contract_testing.contractcase.grpc.ContractCaseConfig} returns this
*/
proto.io.contract_testing.contractcase.grpc.ContractCaseConfig.prototype.clearAutoVersionFrom = function() {
return this.setAutoVersionFrom(undefined);
};


/**
* Returns whether this field is set.
* @return {boolean}
*/
proto.io.contract_testing.contractcase.grpc.ContractCaseConfig.prototype.hasAutoVersionFrom = function() {
return jspb.Message.getField(this, 17) != null;
};





Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ export interface UserNamePassword {
/**
* Configure a ContractCase run. See the [configuration documentation](https://case.contract-testing.io/docs/reference/configuring) for more details.
*
* Note that many of these types are more permissive than the reality - for
* example, the constrained string types are listed here as `string`, whereas
* the core will only accept a limited number of values (eg, log level accepts
* `'warn'` `'error'` etc, but not `'gibbons'`). Callers may rely on the
* boundary mappers to throw `CaseConfigurationErrors` in the case of invalid strings.
*
* @public
*/
export interface ContractCaseBoundaryConfig {
Expand Down Expand Up @@ -146,6 +152,16 @@ export interface ContractCaseBoundaryConfig {
*/
readonly mockConfig?: Record<string, Record<string, unknown>>;

/**
* How to generate version numbers for the system under test
*
* - `"TAG"`: Use {@link https://github.com/absolute-version/absolute-version-js | absolute version} to generate version numbers from the git tag
* - `"GIT_SHA"`: Use the full git sha
*
* @defaultValue `TAG`
*/
readonly autoVersionFrom?: string;

/**
* The internals map allows configuration of low-level ContractCase features.
* It contains no end-user configuration properties, and is intended to be used for customisation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ const mapPublish = (
}
};

const mapAutoVersionFrom = (autoVersionFrom: string): 'TAG' | 'GIT_SHA' => {
switch (autoVersionFrom.toUpperCase()) {
case 'TAG': {
return 'TAG';
}
case 'GIT_SHA':
return 'GIT_SHA';
default:
throw new CaseConfigurationError(
`The autoVersionFrom setting '${autoVersionFrom}' is not a valid auto version setting`,
);
}
};

/**
* SeparateConfig only exists because at one point these two things were separate.
* At some point, we should refactor this so that the config shape doesn't need to
* be mapped into this intermediate type.
*/
type SeparateConfig = {
config: CaseConfig;
partialInvoker: Partial<TestInvoker<AnyMockDescriptorType, unknown>>;
Expand All @@ -54,10 +73,14 @@ export const convertConfig = ({
triggerAndTests,
logLevel,
publish,
autoVersionFrom,
...incoming
}: ContractCaseBoundaryConfig): SeparateConfig => ({
config: {
...incoming,
...(autoVersionFrom
? { autoVersionFrom: mapAutoVersionFrom(autoVersionFrom) }
: {}),
...(logLevel ? { logLevel: mapLogLevel(logLevel) } : {}),
...(publish ? { publish: mapPublish(publish) } : {}),
// baseUrlUnderTest: `http://localhost:${8084}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ const mapAllConfigFields = (
),
triggerAndTest: mapTriggerAndTest(config, executeCall, functionRegistry),
mockConfig: mapMockConfig(config.getMockConfigMap()),

autoVersionFrom: unboxOrUndefined(config.getAutoVersionFrom()),
});

export const mapConfig = (
Expand Down
2 changes: 2 additions & 0 deletions packages/case-connector/src/domain/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export type ContractCaseConnectorConfig = {
triggerAndTest: ConnectorTriggerFunction;

mockConfig: Record<string, Record<string, unknown>>;

autoVersionFrom: string;
};

export type DefinitionId = string;
Expand Down
8 changes: 5 additions & 3 deletions packages/case-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@
"url": "https://github.com/case-contract-testing/case/issues"
},
"devDependencies": {
"@contract-case/case-definition-dsl": "^0.18.0",
"@contract-case/case-maintainer-config": "0.1.0",
"@contract-case/eslint-config-case-maintainer": "0.1.1",
"@contract-case/case-definition-dsl": "^0.18.0",
"@types/current-git-branch": "^1.1.6",
"@types/express": "^4.17.14",
"@types/git-rev-sync": "^2.0.2",
"@types/is-ci": "^3.0.0",
"@types/jest": "^29.5.12",
"@types/qs": "^6.9.16",
Expand All @@ -74,9 +75,9 @@
"typescript": "5.5.4"
},
"dependencies": {
"@contract-case/case-core-plugin-http-dsl": "0.18.0",
"@contract-case/case-core-plugin-http": "0.18.0",
"@contract-case/case-core-plugin-function": "0.18.0",
"@contract-case/case-core-plugin-http": "0.18.0",
"@contract-case/case-core-plugin-http-dsl": "0.18.0",
"@contract-case/case-entities-internal": "0.18.0",
"@contract-case/case-plugin-base": "0.18.0",
"@contract-case/case-plugin-dsl-types": "0.18.0",
Expand All @@ -86,6 +87,7 @@
"chalk": "^4.1.2",
"express": "^4.21.0",
"filenamify": "^4.3.0",
"git-rev-sync": "^3.0.1",
"is-ci": "^3.0.1",
"mkdirp": "^3.0.1",
"pretty-format": "^29.6.1",
Expand Down
1 change: 1 addition & 0 deletions packages/case-core/src/__tests__/testContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const EMPTY_DATA_CONTEXT: DataContext = {
'_case:currentRun:context:testName': '',
'_case:currentRun:context:variables': {},
'_case:currentRun:context:defaultConfig': {},
'_case:currentRun:context:autoVersionFrom': 'TAG',
logger: EMPTY_LOGGER,
resultPrinter: {
printError(): void {},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { EMPTY_DATA_CONTEXT } from '../../__tests__/testContext';
import { makeEnvironment } from './BuildEnvironment';

describe('build environment', () => {
const buildEnvironment = makeEnvironment();

describe('version', () => {
describe('with defaults', () => {
it('starts with a semver tag (this repository always will)', () => {
expect(buildEnvironment.version(EMPTY_DATA_CONTEXT)).toMatch(
/^\d+\.\d+\.\d+/,
);
});
});

describe('with GIT_SHA', () => {
it('should be the right length for a full git sha', () => {
expect(
buildEnvironment.version({
...EMPTY_DATA_CONTEXT,
'_case:currentRun:context:autoVersionFrom': 'GIT_SHA',
}),
).toHaveLength(40);
});
});
});
});
Loading

0 comments on commit dd70365

Please sign in to comment.