Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
boaz-codota committed Feb 14, 2021
1 parent e73efa4 commit 73778f1
Show file tree
Hide file tree
Showing 45 changed files with 740 additions and 619 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/continues_integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
node-version: ${{ matrix.node-version }}
- name: Install
run: npm ci --no-optional
- name: Prettier validate
run: npm run prettier:check
- name: Lint
run: npm run lint
- name: Package
Expand Down
40 changes: 12 additions & 28 deletions package-lock.json

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

25 changes: 13 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,20 @@
],
"main": "./out/extension",
"scripts": {
"vscode:prepublish": "npm i && tsc -p ./",
"compile": "tsc -watch -p ./",
"build": "tsc -p ./",
"vscode:prepublish": "npm i && tsc",
"compile": "tsc -watch",
"build": "tsc",
"test:copyassets": "ncp ./src/test/fixture ./out/test/fixture/",
"prettier": "prettier --write src/",
"lint": "eslint . --ext .js,.ts",
"lint:fix": "eslint . --ext .js,.ts --fix",
"prettier:check": "prettier --check src/",
"lint": "eslint . --max-warnings 0",
"lint:fix": "eslint . --fix",
"vscode:uninstall": "node ./out/uninstall",
"test": "npm run clear-out && tsc -p ./ && npm run test:copyassets && node ./out/test/runTest.js",
"test": "npm run clear-out && tsc && npm run test:copyassets && node ./out/test/runTest.js",
"vsce:package": "vsce package",
"vsce:publish": "vsce publish",
"ovsx:publish": "ovsx publish",
"teamcity:test": "tsc -p ./ && node ./out/test/runTest.js",
"teamcity:test": "tsc && node ./out/test/runTest.js",
"clear-out": "rimraf ./out"
},
"peerDependencies": {
Expand All @@ -113,7 +114,7 @@
"devDependencies": {
"@types/chai": "^4.2.14",
"@types/glob": "^7.1.3",
"@types/mocha": "^8.0.3",
"@types/mocha": "^8.2.0",
"@types/node": "^10.0.0",
"@types/semver": "^7.3.4",
"@types/sinon": "^9.0.8",
Expand All @@ -139,15 +140,15 @@
"prettier": "2.2.1",
"rimraf": "^3.0.2",
"sinon": "^9.2.1",
"typemoq": "^2.1.0",
"typescript": "^4.1.3",
"vsce": "^1.81.1",
"vscode-test": "^1.4.1"
},
"dependencies": {
"await-semaphore": "^0.1.3",
"semver": "^7.3.2",
"tmp": "^0.2.1"
"tmp": "^0.2.1",
"ts-mockito": "^2.6.1"
},
"contributes": {
"languages": [
Expand Down Expand Up @@ -437,10 +438,10 @@
},
"husky": {
"hooks": {
"pre-commit": "lint-staged && npm run lint:fix"
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{js,css,md}": "prettier --write"
"*.{js,css,md}": "prettier --write src/"
}
}
79 changes: 50 additions & 29 deletions src/alphaInstaller.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,100 @@
import * as semver from 'semver';
import {window, commands, Uri, Memento,} from "vscode";
import * as semver from "semver";
import { window, commands, Uri, Memento } from "vscode";
import { Capability, isCapabilityEnabled } from "./capabilities";
import { ALPHA_VERSION_KEY, INSTALL_COMMAND, LATEST_RELEASE_URL, MINIMAL_SUPPORTED_VSCODE_API } from "./consts";
import { downloadFileToDestination, downloadFileToStr } from './download.utils';
import {
ALPHA_VERSION_KEY,
INSTALL_COMMAND,
LATEST_RELEASE_URL,
MINIMAL_SUPPORTED_VSCODE_API,
} from "./consts";
import { downloadFileToDestination, downloadFileToStr } from "./download.utils";
import { tabnineContext } from "./extensionContext";
import createTempFileWithPostfix from './file.utils';
import createTempFileWithPostfix from "./file.utils";

type GitHubAsset = {
browser_download_url: string
}
browser_download_url: string;
};
type GitHubReleaseResponse = {
assets: GitHubAsset[],
}[]
assets: GitHubAsset[];
}[];

export type ExtensionContext = {globalState: Memento}
export type ExtensionContext = { globalState: Memento };

export default async function handleAlpha(context: ExtensionContext): Promise<void> {
export default async function handleAlpha(
context: ExtensionContext
): Promise<void> {
try {

if (userConsumesAlphaVersions()) {

const artifactUrl = await getArtifactUrl();
const availableVersion = getAvailableAlphaVersion(artifactUrl);

if (isNewerAlphaVersionAvailable(context, availableVersion)) {

const { name } = await createTempFileWithPostfix(".vsix");
await downloadFileToDestination(artifactUrl, name);
await commands.executeCommand(INSTALL_COMMAND, Uri.file(name));
await updatePersistedAlphaVersion(context, availableVersion);
console.log(`Tabnine alpha ${availableVersion} has been updated`);
void promptReloadWindow(`TabNine has been updated to ${availableVersion} version. Please reload the window for the changes to take effect.`);

void promptReloadWindow(
`TabNine has been updated to ${availableVersion} version. Please reload the window for the changes to take effect.`
);
}
}
} catch (e) {
console.error(e);
}
}
async function getArtifactUrl() : Promise<string> {
const response = JSON.parse(await downloadFileToStr(LATEST_RELEASE_URL)) as GitHubReleaseResponse;
async function getArtifactUrl(): Promise<string> {
const response = JSON.parse(
await downloadFileToStr(LATEST_RELEASE_URL)
) as GitHubReleaseResponse;
return response[0].assets[0].browser_download_url;
}

function isNewerAlphaVersionAvailable(context: ExtensionContext, availableVersion: string): boolean {
function isNewerAlphaVersionAvailable(
context: ExtensionContext,
availableVersion: string
): boolean {
const currentVersion = getCurrentVersion(context);
const isNewerVersion = !!currentVersion && semver.gt(availableVersion, currentVersion);
const isAlphaAvailable = !!semver.prerelease(availableVersion)?.includes("alpha");
const isSameWithAlphaAvailable = !!currentVersion && semver.eq(semver.coerce(availableVersion)?.version || "", currentVersion) && isAlphaAvailable;
const isNewerVersion =
!!currentVersion && semver.gt(availableVersion, currentVersion);
const isAlphaAvailable = !!semver
.prerelease(availableVersion)
?.includes("alpha");
const isSameWithAlphaAvailable =
!!currentVersion &&
semver.eq(semver.coerce(availableVersion)?.version || "", currentVersion) &&
isAlphaAvailable;

return (isAlphaAvailable && isNewerVersion) || isSameWithAlphaAvailable;
}
function getCurrentVersion(context: ExtensionContext): string | undefined {
const persistedAlphaVersion = getPersistedAlphaVersion(context);
return persistedAlphaVersion || tabnineContext.version;
}
export function getPersistedAlphaVersion(context: ExtensionContext): string | undefined {
export function getPersistedAlphaVersion(
context: ExtensionContext
): string | undefined {
return context.globalState.get<string | undefined>(ALPHA_VERSION_KEY);
}
export function updatePersistedAlphaVersion(context: ExtensionContext, installedVersion: string | undefined): Thenable<void> {
export function updatePersistedAlphaVersion(
context: ExtensionContext,
installedVersion: string | undefined
): Thenable<void> {
return context.globalState.update(ALPHA_VERSION_KEY, installedVersion);
}

function getAvailableAlphaVersion(artifactUrl: string): string{
const versionPattern = /(?<=download\/)(.*)(?=\/tabnine-vscode)/ig;
function getAvailableAlphaVersion(artifactUrl: string): string {
const versionPattern = /(?<=download\/)(.*)(?=\/tabnine-vscode)/gi;
const match = artifactUrl.match(versionPattern);
return match && match[0] || "";
return (match && match[0]) || "";
}
function userConsumesAlphaVersions() : boolean {
function userConsumesAlphaVersions(): boolean {
const isVersionSupported = semver.gte(
tabnineContext.vscodeVersion,
MINIMAL_SUPPORTED_VSCODE_API
);
const isAlpha = isCapabilityEnabled(Capability.ALPHA_CAPABILITY);
return isVersionSupported && isAlpha;

}
async function promptReloadWindow(message: string): Promise<void> {
const reload = "Reload";
Expand Down
Loading

0 comments on commit 73778f1

Please sign in to comment.