Skip to content

Commit

Permalink
Merge pull request #22 from ChuckJonas/ralphcallaway-test_methods
Browse files Browse the repository at this point in the history
Ralphcallaway test methods
  • Loading branch information
ChuckJonas authored Oct 31, 2019
2 parents c3390fd + 5a23c0d commit fe708a8
Show file tree
Hide file tree
Showing 299 changed files with 1,407 additions and 35 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
/package-lock.json
/tmp
node_modules
test/integration/project/*
!test/integration/project/.notgit
test/integration/project/.notgit/logs/HEAD
21 changes: 20 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
// {
// "type": "node",
// "request": "launch",
// "name": "Mocha Tests",
// "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
// "args": [
// "-u",
// "tdd",
// "--timeout",
// "999999",
// "--colors",
// "${workspaceFolder}/test"
// ],
// "internalConsoleOptions": "openOnSessionStart"
// },

{
"type": "node",
"request": "attach",
Expand All @@ -18,11 +34,14 @@
"protocol": "inspector",
"program": "${workspaceRoot}/node_modules/.bin/_mocha",
"args": [
"--require", "test/helpers/init.js",
// "--require", "test/helpers/init.js",
// "--inspect-brk",
"--require", "ts-node/register",
"--require", "source-map-support/register",
"--recursive",
"--reporter", "spec",
"--timeout",
"999999",
"test/**/*.test.ts"
],
"cwd": "${workspaceRoot}"
Expand Down
24 changes: 24 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"cSpell.words": [
"Devhub",
"Integ",
"commandsstop",
"gitpackage",
"globby",
"ignorewhitespace",
"jsdiff",
"loglevel",
"nodelete",
"notgit",
"oclif",
"outputdir",
"postpack",
"posttest",
"shrinkwrap",
"sourceref",
"staticresources",
"stdo",
"targetref",
"tslib"
]
}
38 changes: 30 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

![npm](https://img.shields.io/npm/v/sfdx-git-packager)

Generates a metadata package (`package.xml` & source files) for differences between two branches/commits.
Generates a metadata package (`package.xml` & source files) for differences between two branches/commits.

![Generating vs working tree](https://user-images.githubusercontent.com/5217568/65200914-e587ed80-da45-11e9-917d-a63a3c91b29f.gif)
*Example Generating vs "working tree" & master*
_Example Generating vs "working tree" & master_

## Install

Expand All @@ -23,28 +23,29 @@ Run `sfdx plugins:install sfdx-git-packager`
- CustomObject
- CustomField
- StaticResources
- all other simple *-meta.xml files
- all other simple \*-meta.xml files
- Destructive Changes!

**Not yet supported:**

- ??? (please submit an issue if you run into anything else)
- Windows (needs testing)

## Usage
## Usage

Must be run from inside an sfdx project with an initialized git repo.

<!-- commands -->
* [`sfdx git:package -d <string> [-s <string>] [-t <string>] [-w] [--purge] [--nodelete] [-f] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-gitpackage--d-string--s-string--t-string--w---purge---nodelete--f---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)

- [`sfdx git:package -d <string> [-s <string>] [-t <string>] [-w] [--purge] [--nodelete] [-f] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-gitpackage--d-string--s-string--t-string--w---purge---nodelete--f---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)

## `sfdx git:package -d <string> [-s <string>] [-t <string>] [-w] [--purge] [--nodelete] [-f] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`

Generates a Metadata Package using the differences between two git refs (branch or commit)

```
USAGE
$ sfdx git:package -d <string> [-s <string>] [-t <string>] [-w] [--purge] [--nodelete] [-f] [--json] [--loglevel
$ sfdx git:package -d <string> [-s <string>] [-t <string>] [-w] [--purge] [--nodelete] [-f] [--json] [--loglevel
trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]
OPTIONS
Expand Down Expand Up @@ -83,12 +84,13 @@ EXAMPLES
$ sfdx git:package -s head -d deployments/my-working-copy
```

_See code: [lib/commands/git/package.js](https://github.com/ChuckJonas/sfdx-git-diff-to-pkg/blob/v0.0.4/lib/commands/git/package.js)_
_See code: [lib/commands/git/package.js](https://github.com/ChuckJonas/sfdx-git-diff-to-pkg/blob/v0.0.3/lib/commands/git/package.js)_

<!-- commandsstop -->

### Ignore Files

If you wish to prevent certain files from being included in a package, you can create a `.packageIgnore` in the root of your project. This works similar to [`.gitIgnore`](https://git-scm.com/docs/gitignore). You can add globs to prevent source path from being picked up.
If you wish to prevent certain files from being included in a package, you can create a `.packageIgnore` in the root of your project. This works similar to [`.gitIgnore`](https://git-scm.com/docs/gitignore). You can add globs to prevent source path from being picked up.

## Developing

Expand All @@ -99,7 +101,27 @@ If you wish to prevent certain files from being included in a package, you can c
1. `yarn/npm` install
1. `sfdx plugins:link`

### Testings

`npm test` just runs the basic test suite, not much here yet

#### Integration Testing

`npm run integrationTest` runs integration test suite

#### Setting Up Integration Tests

We've got a git repo in `test/integration/project` that represents a project. In order to avoid conflicts with the parent repo folders we change the .git folder to .notgit so we can commit those to the repo. You'll need to "unpack" that repo if you want to easily work in the test git repo when expanding the integration suite.

To add tests

1. unpack the test repo `npm run tgu`
1. go to the test project `cd test/integration/project`
1. run `git reset --hard` to bring everything back
1. create a branch off of `master`, make the mods you want to test, and commit
1. generate the expected output `npm run gen`
1. check the contents of `test/integration/output` matches what you'd expect for your change (make sure to check there are no other unexpected changes!)
1. pack the test repo back up `npm run tgp`

## Disclaimer

Expand Down
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"@salesforce/command": "^1.4.1",
"@salesforce/core": "^1.3.2",
"diff": "^4.0.1",
"glob-regex": "^0.3.2",
"ignore": "^5.1.4",
"tmp": "^0.1.0",
"tslib": "^1",
Expand All @@ -29,9 +28,11 @@
"@types/tmp": "^0.1.0",
"@types/xml2js": "^0.4.5",
"chai": "^4",
"dir-compare": "^1.7.3",
"globby": "^8",
"mocha": "^5",
"nyc": "^14",
"rimraf": "^3.0.0",
"ts-node": "^8",
"tslint": "^5"
},
Expand Down Expand Up @@ -66,7 +67,11 @@
"postpack": "rm -f oclif.manifest.json",
"posttest": "tslint -p test -t stylish",
"prepack": "rm -rf lib && tsc -b && oclif-dev manifest && oclif-dev readme",
"test": "nyc --extension .ts mocha --forbid-only \"test/**/*.test.ts\"",
"version": "oclif-dev readme && git add README.md"
"test": "nyc --extension .ts mocha --timeout 99999 --forbid-only \"test/{*.test.ts,!(integration)/**/*.test.ts}\"",
"integrationTest": "nyc --extension .ts mocha --timeout 99999 --forbid-only \"test/integration/**/*.test.ts\"",
"version": "oclif-dev readme && git add README.md",
"tgp": "mv test/integration/project/.git test/integration/project/.notgit",
"tgu": "mv test/integration/project/.notgit test/integration/project/.git",
"gen": "ts-node test/integration/genIntegrationTestOutput.ts"
}
}
4 changes: 2 additions & 2 deletions src/commands/git/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default class Package extends SfdxCommand {
public async run(): Promise<AnyJson> {
this.projectPath = this.project.getPath();

this.sourcePaths = ((await this.project.resolveProjectConfig())['packageDirectories'] as any[]).map(d => d.path);
this.sourcePaths = ((await this.project.resolveProjectConfig())['packageDirectories'] as Array<{ path: string }>).map(d => d.path);

const toBranch = this.flags.targetref;
const fromBranch = this.flags.sourceref;
Expand Down Expand Up @@ -177,7 +177,7 @@ export default class Package extends SfdxCommand {
await fs.mkdirp(join(tempDir, sourcePath));
}

await fsPromise.copyFile(join(this.projectPath, 'sfdx-project.json'), join(tempDir, 'sfdx-project.json'));
await copyFileFromRef('sfdx-project.json', targetRef, join(tempDir, 'sfdx-project.json'));

for (const path of changed) {
const metadataPaths = await resolveMetadata(path, targetRef);
Expand Down
4 changes: 2 additions & 2 deletions src/metadataResolvers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { extname } from 'path';
import { getFilesFromRef } from './util';

// these need to be re-witten for windows... maybe use globs instead
// these need to be re-written for windows... maybe use globs instead
const AURA_REGEX = /(.*\/aura\/\w*)\/.*/;
const LWC_REGEX = /(.*\/lwc\/\w*)\/.*/;
const COMP_META = /.*(.cls|\.trigger|\.page|\.component)-meta.xml/;
Expand Down Expand Up @@ -69,7 +69,7 @@ const metadataResolvers: MetadataResolver[] = [
match: STATIC_RESOURCE_FILE_REGEX,
getMetadataPaths: async (path: string) => {
const baseName = STATIC_RESOURCE_FILE_REGEX.exec(path)[1];
return [path, `${baseName}.resource-meta.xml` ];
return [path, `${baseName}.resource-meta.xml`];
},
getIsDirectory: () => false
}
Expand Down
19 changes: 0 additions & 19 deletions test/commands/hello/org.test.ts

This file was deleted.

5 changes: 5 additions & 0 deletions test/future.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('test suite', () => {
it('will have some tests in the future', () => {
console.log('but not yet');
});
});
16 changes: 16 additions & 0 deletions test/integration/genIntegrationTestOutput.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { getIntegBranches, myExec, projectPath } from './util';

(async () => {
try {
for (const branch of await getIntegBranches()) {
console.log(`generating ${branch}`);
await myExec(
`sfdx git:package -s ${branch} -t master -d "../output/${branch}" --purge`,
projectPath);
}
} catch (e) {
console.log(e);
}
})()
.then(() => console.log('done'))
.catch(e => console.log(e));
86 changes: 86 additions & 0 deletions test/integration/integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import * as assert from 'assert';
import { compareSync } from 'dir-compare';
import { myExec, projectPath, setGitENV } from './util';

async function runTest(testName: string) {
const sourceRef = testName;
const expectedOutputDir = testName;
try {
const res = await myExec(
`sfdx git:package -d deploy --purge -s ${sourceRef} -t master`,
projectPath);
assert.equal(null, res.err);
const compareRes = compareSync('test/integration/project/deploy', `test/integration/output/${expectedOutputDir}`, { compareContent: true });
const mismatched = compareRes.diffSet.filter(diff => diff.state !== 'equal').map(diff => diff.name1 || diff.name2);
assert.strictEqual(mismatched.length, 0, `The following files were different: \n${mismatched.join('\n')}`);
assert.strictEqual(true, compareRes.equal > 0);
} catch (e) {
assert.fail(e);
}
}

describe('git:package integration test', async () => {
before(async () => {
setGitENV();
await myExec('git checkout master', projectPath);
await myExec('git reset --hard', projectPath);
});

describe('apex classes', async () => {
it('detects a new apex class', async () => {
await runTest('add_class');
});
it('detects an update to an apex class', async () => {
await runTest('mod_class');
});
it('detects an update to an apex class meta file', async () => {
await runTest('mod_class_meta');
});
it('detects deletion of an apex class', async () => {
await runTest('del_class');
});
});

describe('custom fields', async () => {
it('detects a new custom field', async () => {
await runTest('add_field');
});
it('detects an update to an custom field', async () => {
await runTest('mod_field');
});
it('detects deletion of an custom field', async () => {
await runTest('del_field');
});
});

describe('custom objects', async () => {
it('detects a new custom object', async () => {
await runTest('add_object');
});
it('detects an update to an custom object', async () => {
await runTest('mod_object');
});
it('detects deletion of an custom object', async () => {
await runTest('del_object');
});
});

describe('static resources', async () => {
it('detects a new static resource', async () => {
await runTest('add_static_resource');
});
it('detects an update to a static resource', async () => {
await runTest('mod_static_resource');
});
it('detects full deletion of a static resource', async () => {
await runTest('del_static_resource');
});
});

describe('folder_types', async () => {
it('detects partial deletion of a folder', async () => {
await runTest('del_single_file_from_folder');
});
});

});
5 changes: 5 additions & 0 deletions test/integration/output/add_class/classes/Example2.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public with sharing class Example2 {
public Example2() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="urn:metadata.tooling.soap.sforce.com" fqn="Example2">
<apiVersion>47.0</apiVersion>
<status>Active</status>
</ApexClass>
8 changes: 8 additions & 0 deletions test/integration/output/add_class/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<name>ApexClass</name>
<members>Example2</members>
</types>
<version>45.0</version>
</Package>
11 changes: 11 additions & 0 deletions test/integration/output/add_field/objects/Foo__c.object
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
<fields>
<fullName>A_Date__c</fullName>
<externalId>false</externalId>
<label>A Date</label>
<required>false</required>
<trackTrending>false</trackTrending>
<type>Date</type>
</fields>
</CustomObject>
Loading

0 comments on commit fe708a8

Please sign in to comment.