<commit_before>/** @babel */

import season from 'season'
import dedent from 'dedent'
import electron from 'electron'
import fs from 'fs-plus'
import path from 'path'
import AtomApplication from '../../src/main-process/atom-application'
import parseCommandLine from '../../src/main-process/parse-command-line'
import {timeoutPromise, conditionPromise} from '../async-spec-helpers'

const ATOM_RESOURCE_PATH = path.resolve(__dirname, '..', '..')

describe('AtomApplication', function () {
  this.timeout(60 * 1000)

  let originalAppQuit, originalAtomHome, atomApplicationsToDestroy

  beforeEach(function () {
    originalAppQuit = electron.app.quit
    mockElectronAppQuit()
    originalAtomHome = process.env.ATOM_HOME
    process.env.ATOM_HOME = makeTempDir('atom-home')
    // Symlinking the compile cache into the temporary home dir makes the windows load much faster
    fs.symlinkSync(path.join(originalAtomHome, 'compile-cache'), path.join(process.env.ATOM_HOME, 'compile-cache'), 'junction')
    season.writeFileSync(path.join(process.env.ATOM_HOME, 'config.cson'), {
      '*': {
        welcome: {showOnStartup: false},
        core: {telemetryConsent: 'no'}
      }
    })
    atomApplicationsToDestroy = []
  })

  afterEach(async function () {
    process.env.ATOM_HOME = originalAtomHome
    for (let atomApplication of atomApplicationsToDestroy) {
      await atomApplication.destroy()
    }
    await clearElectronSession()
    electron.app.quit = originalAppQuit
  })

  describe('project paths', function () {
    it('sync application state on changes', async function () {
      const dirA = makeTempDir()
      const dirB = makeTempDir()
      const atomApplication = buildAtomApplication()

      let cnt = 0
      const origSaveState = atomApplication.saveState
      const mockSaveState = function () {
        cnt++
        origSaveState.apply(atomApplication, arguments)
      }

      const window = atomApplication.launch(parseCommandLine([]))
      await focusWindow(window)

      const addProjectPathFn = function (dir) {
        return 'function (sendBackToMainProcess) { atom.project.addPath("' + dir + '"); sendBackToMainProcess(null); }'
      }
      const removeProjectPathFn = function (dir) {
        return 'function (sendBackToMainProcess) { atom.project.removePath("' + dir + '"); sendBackToMainProcess(null); }'
      }

      atomApplication.saveState = mockSaveState
      await evalInWebContents(window.browserWindow.webContents, addProjectPathFn(dirA))
      assert.equal(cnt, 1)
      await evalInWebContents(window.browserWindow.webContents, addProjectPathFn(dirB))
      assert.equal(cnt, 2)
      await evalInWebContents(window.browserWindow.webContents, removeProjectPathFn(dirA))
      assert.equal(cnt, 3)
    })
  })

  describe('launch', function () {
    it('can open to a specific line number of a file', async function () {
      const filePath = path.join(makeTempDir(), 'new-file')
      fs.writeFileSync(filePath, '1\n2\n3\n4\n')
      const atomApplication = buildAtomApplication()

      const window = atomApplication.launch(parseCommandLine([filePath + ':3']))
      await focusWindow(window)

      const cursorRow = await evalInWebContents(window.browserWindow.webContents, function (sendBackToMainProcess) {
        atom.workspace.observeActivePaneItem(function (textEditor) {
          if (textEditor) sendBackToMainProcess(textEditor.getCursorBufferPosition().row)
        })
      })

      assert.equal(cursorRow, 2)
    })

    it('can open to a specific line and column of a file', async function () {
      const filePath = path.join(makeTempDir(), 'new-file')
      fs.writeFileSync(filePath, '1\n2\n3\n4\n')
      const atomApplication = buildAtomApplication()
<commit_msg>Another try to fix tests on Windows.<commit_after>/** @babel */

import season from 'season'
import dedent from 'dedent'
import electron from 'electron'
import fs from 'fs-plus'
import path from 'path'
import AtomApplication from '../../src/main-process/atom-application'
import parseCommandLine from '../../src/main-process/parse-command-line'
import {timeoutPromise, conditionPromise} from '../async-spec-helpers'

const ATOM_RESOURCE_PATH = path.resolve(__dirname, '..', '..')

describe('AtomApplication', function () {
  this.timeout(60 * 1000)

  let originalAppQuit, originalAtomHome, atomApplicationsToDestroy

  beforeEach(function () {
    originalAppQuit = electron.app.quit
    mockElectronAppQuit()
    originalAtomHome = process.env.ATOM_HOME
    process.env.ATOM_HOME = makeTempDir('atom-home')
    // Symlinking the compile cache into the temporary home dir makes the windows load much faster
    fs.symlinkSync(path.join(originalAtomHome, 'compile-cache'), path.join(process.env.ATOM_HOME, 'compile-cache'), 'junction')
    season.writeFileSync(path.join(process.env.ATOM_HOME, 'config.cson'), {
      '*': {
        welcome: {showOnStartup: false},
        core: {telemetryConsent: 'no'}
      }
    })
    atomApplicationsToDestroy = []
  })

  afterEach(async function () {
    process.env.ATOM_HOME = originalAtomHome
    for (let atomApplication of atomApplicationsToDestroy) {
      await atomApplication.destroy()
    }
    await clearElectronSession()
    electron.app.quit = originalAppQuit
  })

  describe('project paths', function () {
    it('sync application state on changes', async function () {
      const dirA = makeTempDir()
      const dirB = makeTempDir()
      const atomApplication = buildAtomApplication()

      let cnt = 0
      const origSaveState = atomApplication.saveState
      const mockSaveState = function () {
        cnt++
        origSaveState.apply(atomApplication, arguments)
      }

      const window = atomApplication.launch(parseCommandLine([]))
      await focusWindow(window)

      const addProjectPathFn = function (dir) {
        return 'function (sendBackToMainProcess) { atom.project.addPath("' + dir + '"); sendBackToMainProcess(null); }'
      }
      const removeProjectPathFn = function (dir) {
        return 'function (sendBackToMainProcess) { atom.project.removePath("' + dir + '"); sendBackToMainProcess(null); }'
      }

      atomApplication.saveState = mockSaveState
      await evalInWebContents(window.browserWindow.webContents, addProjectPathFn(dirA))
      await conditionPromise(() => cnt === 1)
      assert.equal(cnt, 1)
      await evalInWebContents(window.browserWindow.webContents, addProjectPathFn(dirB))
      await conditionPromise(() => cnt === 2)
      assert.equal(cnt, 2)
      await evalInWebContents(window.browserWindow.webContents, removeProjectPathFn(dirA))
      await conditionPromise(() => cnt === 3)
      assert.equal(cnt, 3)
    })
  })

  describe('launch', function () {
    it('can open to a specific line number of a file', async function () {
      const filePath = path.join(makeTempDir(), 'new-file')
      fs.writeFileSync(filePath, '1\n2\n3\n4\n')
      const atomApplication = buildAtomApplication()

      const window = atomApplication.launch(parseCommandLine([filePath + ':3']))
      await focusWindow(window)

      const cursorRow = await evalInWebContents(window.browserWindow.webContents, function (sendBackToMainProcess) {
        atom.workspace.observeActivePaneItem(function (textEditor) {
          if (textEditor) sendBackToMainProcess(textEditor.getCursorBufferPosition().row)
        })
      })

      assert.equal(cursorRow, 2)
    })

    it('can open to a specific line and column of a file', async function () {
      const filePath = path.join(makeTempDir(), 'new-file')
      fs.writeFileSync(filePath, '1\n2\n3\n4\n')
      const atomApplication = buildAtomApplication()

{'fraction_non_alphanumeric': 0.11692548610204881, 'fraction_numerical': 0.00561137935534386, 'mean_word_length': 3.650485436893204, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4218063', 'n_tokens_mistral': 2254, 'n_tokens_neox': 2201, 'n_words': 488}