Best JavaScript code snippet using stryker-parent
Journal.ts
Source:Journal.ts
1import { constants } from 'fs';2import { access, open } from 'fs/promises';3import { DateTime } from 'luxon';4const FULL_BACKUP_FILE_NAME_SUFFIX = "-full.sql.enc";5const DELTA_BACKUP_FILE_NAME_SUFFIX = "-delta.sql.enc";6const FULL_LEGACY_BACKUP_FILE_NAME_PREFIX = "dump_";7const LEGACY_DATE_LENGTH = 'YYYY-MM-DD'.length;8export type BackupFileNameType = 'FULL' | 'DELTA' | 'FULL_LEGACY';9export class BackupFileName {10 static getFullBackupFileName(date: DateTime): BackupFileName {11 return new BackupFileName({12 date: date,13 type: 'FULL'14 });15 }16 static getDeltaBackupFileName(date: DateTime): BackupFileName {17 return new BackupFileName({18 date: date,19 type: 'DELTA'20 });21 }22 static getLegacyFullBackupFileName(date: DateTime): BackupFileName {23 return new BackupFileName({24 date: date,25 type: 'FULL_LEGACY'26 });27 }28 static parse(fileName: string): BackupFileName {29 if(fileName.endsWith(FULL_BACKUP_FILE_NAME_SUFFIX)) {30 const dateString = fileName.substring(0, fileName.length - FULL_BACKUP_FILE_NAME_SUFFIX.length);31 return new BackupFileName({32 date: DateTime.fromISO(dateString),33 type: 'FULL'34 });35 } else if(fileName.endsWith(DELTA_BACKUP_FILE_NAME_SUFFIX)) {36 const dateString = fileName.substring(0, fileName.length - DELTA_BACKUP_FILE_NAME_SUFFIX.length);37 return new BackupFileName({38 date: DateTime.fromISO(dateString),39 type: 'DELTA'40 });41 } else if(fileName.startsWith(FULL_LEGACY_BACKUP_FILE_NAME_PREFIX)) {42 const dateString = fileName.substring(FULL_LEGACY_BACKUP_FILE_NAME_PREFIX.length, FULL_LEGACY_BACKUP_FILE_NAME_PREFIX.length + LEGACY_DATE_LENGTH);43 const timeString = fileName.substring(FULL_LEGACY_BACKUP_FILE_NAME_PREFIX.length + LEGACY_DATE_LENGTH + 1, fileName.length - 8);44 const timeElements = timeString.split("-");45 const dateTimeString = `${dateString}T${timeElements.join(":")}`;46 return new BackupFileName({47 date: DateTime.fromISO(dateTimeString, {zone: 'utc'}),48 type: 'FULL_LEGACY'49 });50 } else {51 throw new Error("Bad file name format");52 }53 }54 constructor(args: {55 date: DateTime,56 type: BackupFileNameType57 }) {58 this.date = args.date;59 this.type = args.type;60 }61 readonly date: DateTime;62 readonly type: BackupFileNameType;63 get fileName(): string {64 if(this.type === 'FULL') {65 return `${this.date.toISO()}${FULL_BACKUP_FILE_NAME_SUFFIX}`;66 } else if(this.type === 'FULL_LEGACY') {67 const isoDate = this.date.toISO();68 const legacyDate = isoDate.slice(0, isoDate.length - 8).replace("T", "-").replace(":", "-");69 return `${FULL_LEGACY_BACKUP_FILE_NAME_PREFIX}${legacyDate}.sql.enc`;70 } else {71 return `${this.date.toISO()}${DELTA_BACKUP_FILE_NAME_SUFFIX}`;72 }73 }74}75export class BackupFile {76 constructor(args: {77 cid: string,78 fileName: BackupFileName,79 }) {80 this.cid = args.cid;81 this.fileName = args.fileName;82 }83 readonly cid: string;84 readonly fileName: BackupFileName;85}86export class Journal implements Iterable<BackupFile> {87 static async read(path: string): Promise<Journal> {88 const journal = new Journal();89 const file = await open(path, 'r');90 const content = (await file.readFile()).toString("utf-8");91 const lines = content.split(/\r?\n/);92 await file.close();93 for(const line of lines) {94 const elements = line.split(/[ \t]+/);95 if(elements.length === 2) {96 const cid = elements[0];97 const fileName = BackupFileName.parse(elements[1]);98 journal.addBackup(new BackupFile({99 cid,100 fileName101 }));102 } else if(elements.length === 3) {103 const cid = elements[1];104 const fileName = BackupFileName.parse(elements[2]);105 journal.addBackup(new BackupFile({106 cid,107 fileName108 }));109 }110 }111 return journal;112 }113 addBackup(backup: BackupFile) {114 this.backupFiles.push(backup);115 }116 private backupFiles: BackupFile[] = [];117 async write(path: string) {118 const file = await open(path, 'w');119 for(const backupFile of this.backupFiles) {120 await file.write(Buffer.from(`${backupFile.cid} ${backupFile.fileName.fileName}\n`, "utf-8"));121 }122 await file.close();123 }124 isEmpty(): boolean {125 return this.backupFiles.length === 0;126 }127 getLastFullBackup(): BackupFile | undefined {128 const index = this.getLastFullBackupIndex();129 if(index >= 0) {130 return this.backupFiles[index];131 } else {132 return undefined;133 }134 }135 private getLastFullBackupIndex(): number {136 for(let i = this.backupFiles.length - 1; i >= 0; --i) {137 const backupFile = this.backupFiles[i];138 if(backupFile.fileName.type === 'FULL') {139 return i;140 }141 }142 return -1;143 }144 keepOnlyLastFullBackups(maxFullBackups: number): BackupFile[] {145 if(this.backupFiles.length === 0) {146 return [];147 } else {148 let fullBackupIndex = this.getPreviousFullBackup(this.backupFiles.length - 1);149 let fullBackups;150 if(fullBackupIndex > 0) {151 fullBackups = 1;152 } else {153 fullBackups = 0;154 }155 while(fullBackupIndex >= 0 && fullBackups < maxFullBackups) {156 fullBackupIndex = this.getPreviousFullBackup(fullBackupIndex - 1);157 if(fullBackupIndex > 0) {158 ++fullBackups;159 }160 }161 if(fullBackupIndex > 0) {162 const toRemove = this.backupFiles.slice(0, fullBackupIndex);163 this.backupFiles = this.backupFiles.slice(fullBackupIndex);164 return toRemove;165 } else {166 return [];167 }168 }169 }170 private getPreviousFullBackup(current: number): number {171 let next = current;172 while(next >= 0 && this.backupFiles[next].fileName.type === 'DELTA') {173 --next;174 }175 if(next === 0 && this.backupFiles[next].fileName.type === 'DELTA') {176 return -1;177 } else {178 return next;179 }180 }181 getRecoveryPath(): BackupFile[] {182 const index = this.getLastFullBackupIndex();183 if(index < 0) {184 throw new Error("No recovery path");185 } else {186 return this.backupFiles.slice(index);187 }188 }189 [Symbol.iterator](): Iterator<BackupFile, BackupFile> {190 return this.backupFiles[Symbol.iterator]();191 }192}193export async function readJournalOrNew(journalFile: string): Promise<Journal> {194 try {195 await access(journalFile, constants.F_OK);196 return await Journal.read(journalFile);197 } catch {198 return new Journal();199 }...
Journal.spec.ts
Source:Journal.spec.ts
1import { DateTime } from "luxon";2import { BackupFile, BackupFileName, Journal } from "../src/Journal";3describe("BackupFileName", () => {4 it("parses full backup file name", () => {5 const dateTime = DateTime.now();6 const fileName = BackupFileName.parse(`${dateTime.toISO()}-full.sql.enc`);7 expect(fileName.date).toEqual(dateTime);8 expect(fileName.type).toEqual('FULL');9 });10 it("parses delta backup file name", () => {11 const dateTime = DateTime.now();12 const fileName = BackupFileName.parse(`${dateTime.toISO()}-delta.sql.enc`);13 expect(fileName.date).toEqual(dateTime);14 expect(fileName.type).toEqual('DELTA');15 });16 it("parses full legacy backup file name", () => {17 const date = DateTime.fromISO("2022-04-07T14:56:14", {zone: 'utc'});18 const fileName = BackupFileName.parse(`dump_2022-04-07-14-56-14.sql.enc`);19 expect(fileName.date).toEqual(date);20 expect(fileName.type).toEqual('FULL_LEGACY');21 });22 it("generates full backup file name", () => {23 const date = DateTime.now();24 const backupFileName = new BackupFileName({25 date,26 type: 'FULL'27 });28 expect(backupFileName.fileName).toBe(`${date.toISO()}-full.sql.enc`);29 });30 it("generates delta backup file name", () => {31 const date = DateTime.now();32 const backupFileName = new BackupFileName({33 date,34 type: 'DELTA'35 });36 expect(backupFileName.fileName).toBe(`${date.toISO()}-delta.sql.enc`);37 });38 it("generates full legacy backup file name", () => {39 const date = DateTime.fromISO("2022-04-07T14:56:14", {zone: 'utc'});40 const backupFileName = new BackupFileName({41 date,42 type: 'FULL_LEGACY'43 });44 expect(backupFileName.fileName).toBe(`dump_2022-04-07-14-56.sql.enc`);45 });46});47describe("Journal", () => {48 it("is empty with empty file", async () => {49 const journal = await Journal.read("test/empty.txt");50 expect(journal.isEmpty()).toBe(true);51 });52 it("contains expected entries", async () => {53 const journal = await Journal.read("test/journal.txt");54 expect(journal.isEmpty()).toBe(false);55 const expectedBackupFiles = [56 new BackupFile({57 cid: "cid0",58 fileName: BackupFileName.getFullBackupFileName(DateTime.fromISO("2022-04-07T14:56:14.326+02:00"))59 }),60 new BackupFile({61 cid: "cid1",62 fileName: BackupFileName.getDeltaBackupFileName(DateTime.fromISO("2022-04-07T14:57:14.326+02:00"))63 }),64 new BackupFile({65 cid: "cid2",66 fileName: BackupFileName.getFullBackupFileName(DateTime.fromISO("2022-04-07T14:58:14.326+02:00"))67 })68 ];69 expectJournalToHaveExpectedFiles(journal, expectedBackupFiles);70 });71 it("finds last full backup", async () => {72 const journal = await Journal.read("test/journal.txt");73 expect(journal.isEmpty()).toBe(false);74 const expectedBackupFile = new BackupFile({75 cid: "cid2",76 fileName: BackupFileName.getFullBackupFileName(DateTime.fromISO("2022-04-07T14:58:14.326+02:00"))77 });78 expect(journal.getLastFullBackup()).toEqual(expectedBackupFile);79 });80 it("reads legacy entries", async () => {81 const journal = await Journal.read("test/legacy_journal.txt");82 expect(journal.isEmpty()).toBe(false);83 const expectedBackupFiles = [84 new BackupFile({85 cid: "cid0",86 fileName: BackupFileName.getLegacyFullBackupFileName(DateTime.fromISO("2022-04-07T01:00:01.000", {zone: 'utc'}))87 }),88 new BackupFile({89 cid: "cid1",90 fileName: BackupFileName.getLegacyFullBackupFileName(DateTime.fromISO("2022-04-08T01:00:01.000", {zone: 'utc'}))91 }),92 new BackupFile({93 cid: "cid2",94 fileName: BackupFileName.getDeltaBackupFileName(DateTime.fromISO("2022-04-09T14:57:14.326+02:00"))95 })96 ];97 expectJournalToHaveExpectedFiles(journal, expectedBackupFiles);98 });99});100function expectJournalToHaveExpectedFiles(journal: Journal, expectedBackupFiles: BackupFile[]) {101 let i = 0;102 for(const backupFile of journal) {103 expect(backupFile).toEqual(expectedBackupFiles[i]);104 ++i;105 }...
test_utils_backups_create.js
Source:test_utils_backups_create.js
1/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */2/* vim:set ts=2 sw=2 sts=2 et: */3/* This Source Code Form is subject to the terms of the Mozilla Public4 * License, v. 2.0. If a copy of the MPL was not distributed with this5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */6 /**7 * Check for correct functionality of PlacesUtils.archiveBookmarksFile8 */9const PREFIX = "bookmarks-";10// The localized prefix must be "bigger" and associated to older backups.11const LOCALIZED_PREFIX = "segnalibri-";12const SUFFIX = ".json";13const NUMBER_OF_BACKUPS = 10;14function run_test() {15 do_test_pending();16 // Generate random dates.17 var dateObj = new Date();18 var dates = [];19 while (dates.length < NUMBER_OF_BACKUPS) {20 // Use last year to ensure today's backup is the newest.21 let randomDate = new Date(dateObj.getFullYear() - 1,22 Math.floor(12 * Math.random()),23 Math.floor(28 * Math.random()));24 let dateString = randomDate.toLocaleFormat("%Y-%m-%d");25 if (dates.indexOf(dateString) == -1)26 dates.push(dateString);27 }28 // Sort dates from oldest to newest.29 dates.sort();30 // Get and cleanup the backups folder.31 var bookmarksBackupDir = PlacesBackups.folder;32 // Fake backups are created backwards to ensure we won't consider file33 // creation time.34 // Create fake backups for the newest dates.35 for (let i = dates.length - 1; i >= 0; i--) {36 let backupFilename;37 if (i > Math.floor(dates.length/2))38 backupFilename = PREFIX + dates[i] + SUFFIX;39 else40 backupFilename = LOCALIZED_PREFIX + dates[i] + SUFFIX;41 dump("creating: " + backupFilename + "\n");42 let backupFile = bookmarksBackupDir.clone();43 backupFile.append(backupFilename);44 backupFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);45 do_check_true(backupFile.exists());46 }47 48 // Replace PlacesUtils getFormattedString so that it will return the localized49 // string we want.50 PlacesUtils.getFormattedString = function (aKey, aValue) {51 return LOCALIZED_PREFIX + aValue;52 }53 Task.spawn(function() {54 yield PlacesBackups.create(Math.floor(dates.length/2));55 // Add today's backup.56 dates.push(dateObj.toLocaleFormat("%Y-%m-%d"));57 // Check backups.58 for (var i = 0; i < dates.length; i++) {59 let backupFilename;60 let shouldExist;61 let backupFile;62 if (i > Math.floor(dates.length/2)) {63 let files = bookmarksBackupDir.directoryEntries;64 let rx = new RegExp("^" + PREFIX + dates[i] + "(_[0-9]+){0,1}" + SUFFIX + "$");65 while (files.hasMoreElements()) {66 let entry = files.getNext().QueryInterface(Ci.nsIFile);67 if (entry.leafName.match(rx)) {68 backupFilename = entry.leafName;69 backupFile = entry;70 break;71 }72 }73 shouldExist = true;74 }75 else {76 backupFilename = LOCALIZED_PREFIX + dates[i] + SUFFIX;77 backupFile = bookmarksBackupDir.clone();78 backupFile.append(backupFilename);79 shouldExist = false;80 }81 if (backupFile.exists() != shouldExist)82 do_throw("Backup should " + (shouldExist ? "" : "not") + " exist: " + backupFilename);83 }84 // Cleanup backups folder.85 // XXX: Can't use bookmarksBackupDir.remove(true) because file lock happens86 // on WIN XP.87 let files = bookmarksBackupDir.directoryEntries;88 while (files.hasMoreElements()) {89 let entry = files.getNext().QueryInterface(Ci.nsIFile);90 entry.remove(false);91 }92 do_check_false(bookmarksBackupDir.directoryEntries.hasMoreElements());93 // Recreate the folder.94 PlacesBackups.folder;95 do_test_finished();96 });...
Using AI Code Generation
1var backupFileName = require('stryker-parent').backupFileName;2backupFileName('test.js');3var backupFileName = require('stryker-parent').backupFileName;4backupFileName('test.js');5var backupFileName = require('stryker-parent').backupFileName;6backupFileName('test.js');7var backupFileName = require('stryker-parent').backupFileName;8backupFileName('test.js');9var backupFileName = require('stryker-parent').backupFileName;10backupFileName('test.js');11var backupFileName = require('stryker-parent').backupFileName;12backupFileName('test.js');13var backupFileName = require('stryker-parent').backupFileName;14backupFileName('test.js');15var backupFileName = require('stryker-parent').backupFileName;16backupFileName('test.js');17var backupFileName = require('stryker-parent').backupFileName;18backupFileName('test.js');19var backupFileName = require('stryker-parent').backupFileName;20backupFileName('test.js');21var backupFileName = require('stryker-parent').backupFileName;22backupFileName('test.js');23var backupFileName = require('stryker-parent').backupFileName;24backupFileName('test.js');25var backupFileName = require('stryker-parent').backupFileName;26backupFileName('test.js');
Using AI Code Generation
1const strykerParent = require('stryker-parent');2const strykerParent = require('stryker-parent');3const strykerParent = require('stryker-parent');4const strykerParent = require('stryker-parent');5const strykerParent = require('stryker-parent');6const strykerParent = require('stryker-parent');7const strykerParent = require('stryker-parent');8const strykerParent = require('stryker-parent');9const strykerParent = require('stryker-parent');10const strykerParent = require('stryker-parent');11const strykerParent = require('stryker-parent');12const strykerParent = require('stryker-parent');13const strykerParent = require('stryker-parent');14const strykerParent = require('stryker-parent');15const strykerParent = require('stryker-parent');16const strykerParent = require('stryker-parent');17const strykerParent = require('stryker-parent');18const strykerParent = require('stryker-parent');
Using AI Code Generation
1var backupFileName = require('stryker-parent').backupFileName;2var fileName = 'test.js';3var result = backupFileName(fileName);4console.log(result);5var backupFileName = require('stryker-parent').backupFileName;6var fileName = 'test.js';7var result = backupFileName(fileName);8console.log(result);9var backupFileName = require('stryker-parent').backupFileName;10var fileName = 'test.js';11var result = backupFileName(fileName);12console.log(result);13var backupFileName = require('stryker-parent').backupFileName;14var fileName = 'test.js';15var result = backupFileName(fileName);16console.log(result);17var backupFileName = require('stryker-parent').backupFileName;18var fileName = 'test.js';19var result = backupFileName(fileName);20console.log(result);21var backupFileName = require('stryker-parent').backupFileName;22var fileName = 'test.js';23var result = backupFileName(fileName);24console.log(result);25var backupFileName = require('stryker-parent').backupFileName;26var fileName = 'test.js';27var result = backupFileName(fileName);28console.log(result);29var backupFileName = require('stryker-parent').backupFileName;30var fileName = 'test.js';31var result = backupFileName(fileName);32console.log(result);33var backupFileName = require('stryker-parent').backupFileName;34var fileName = 'test.js';35var result = backupFileName(fileName);36console.log(result);
Using AI Code Generation
1var stryker = require('stryker-parent');2var strykerConfig = stryker.readConfig();3var backupFileName = stryker.backupFileName(strykerConfig);4console.log(backupFileName);5module.exports = function(config) {6 config.set({7 });8};9module.exports = function(config) {10 config.set({11 backupFileName: function() {12 return 'stryker.bak.js';13 }14 });15};16module.exports = function(config) {17 config.set({18 backupFileName: function() {19 return 'stryker.bak.js';20 }21 });22};23module.exports = function(config) {24 config.set({25 backupFileName: function() {26 return 'stryker.bak.js';27 }28 });29};30module.exports = function(config) {31 config.set({32 backupFileName: function() {33 return 'stryker.bak.js';34 }35 });36};37module.exports = function(config) {38 config.set({39 backupFileName: function() {40 return 'stryker.bak.js';41 }42 });43};44module.exports = function(config) {45 config.set({46 backupFileName: function() {47 return 'stryker.bak.js';48 }49 });50};51module.exports = function(config) {52 config.set({53 backupFileName: function() {54 return 'stryker.bak.js';55 }56 });57};58module.exports = function(config) {59 config.set({60 backupFileName: function() {61 return 'stryker.bak.js';62 }63 });64};65module.exports = function(config) {66 config.set({67 backupFileName: function() {68 return 'stryker.bak.js';69 }70 });71};72module.exports = function(config) {73 config.set({
Using AI Code Generation
1var strykerParent = require('stryker-parent');2var backupFileName = strykerParent.backupFileName;3var fileName = backupFileName('test.js');4var strykerParent = require('stryker-parent');5var backupFileName = strykerParent.backupFileName;6var fileName = backupFileName('stryker.conf.js');7var strykerParent = require('stryker-parent');8var backupFileName = strykerParent.backupFileName;9var fileName = backupFileName('stryker.conf.js');10var strykerParent = require('stryker-parent');11var backupFileName = strykerParent.backupFileName;12var fileName = backupFileName('stryker.conf.js');13var strykerParent = require('stryker-parent');14var backupFileName = strykerParent.backupFileName;15var fileName = backupFileName('stryker.conf.js');16var strykerParent = require('stryker-parent');17var backupFileName = strykerParent.backupFileName;18var fileName = backupFileName('stryker.conf.js');19var strykerParent = require('stryker-parent');20var backupFileName = strykerParent.backupFileName;
Using AI Code Generation
1var stryker = require('stryker-parent');2var backup = stryker.backupFileName('test.js');3console.log(backup);4var stryker = require('stryker-parent');5var backup = stryker.backupFileName('src/test.js');6console.log(backup);7var stryker = require('stryker-parent');8var backup = stryker.backupFileName('src/test.js', 'stryker');9console.log(backup);10var stryker = require('stryker-parent');11var backup = stryker.backupFileName('src/test.js', 'stryker', 'tmp');12console.log(backup);13var stryker = require('stryker-parent');14var backup = stryker.backupFileName('src/test.js', 'stryker', 'tmp', 5678);15console.log(backup);16var stryker = require('stryker-parent');17var backup = stryker.backupFileName('src/test.js', 'stryker', 'tmp', 5678, true);18console.log(backup);19var stryker = require('stryker-parent');20var backup = stryker.backupFileName('src/test.js', 'stryker', 'tmp', 5678, false);21console.log(backup);
Using AI Code Generation
1var stryker = require('stryker-parent');2var fileName = stryker.backupFileName('folder/file.txt');3console.log(fileName);4var stryker = require('stryker');5var fileName = stryker.backupFileName('folder/file.txt');6console.log(fileName);7var stryker = require('stryker-api');8var fileName = stryker.backupFileName('folder/file.txt');9console.log(fileName);10var stryker = require('stryker');11var fileName = stryker.backupFileName('folder/file.txt');12console.log(fileName);13var stryker = require('stryker-api');14var fileName = stryker.backupFileName('folder/file.txt');15console.log(fileName);16var stryker = require('stryker');17var fileName = stryker.backupFileName('folder/file.txt');18console.log(fileName);19var stryker = require('stryker-api');20var fileName = stryker.backupFileName('folder/file.txt');21console.log(fileName);22var stryker = require('stryker');23var fileName = stryker.backupFileName('folder/file.txt');24console.log(fileName);25var stryker = require('stryker-api');26var fileName = stryker.backupFileName('folder/file.txt');27console.log(fileName);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!