Skip to content

Commit

Permalink
fix: Handle paths for accessibility (#14054)
Browse files Browse the repository at this point in the history
* fix: Handle paths for accessibility

* refactror: Fix UT

* test: Add more Unit tests

* test: Fix linters

---------

Co-authored-by: Christian Bromann <git@bromann.dev>
Co-authored-by: Kamalpreet Kaur <38219887+kamal-kaur04@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 28, 2025
1 parent 4a5a4ba commit d57358e
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ class AccessibilityScripts {
public saveTestResults: string | null = null
public commandsToWrap: Array<Command> | null = null

public browserstackFolderPath = path.join(os.homedir(), '.browserstack')
public commandsPath = path.join(this.browserstackFolderPath, 'commands.json')
public browserstackFolderPath = ''
public commandsPath = ''

// don't allow to create instances from it other than through `checkAndGetInstance`
private constructor() {}
private constructor() {
this.browserstackFolderPath = this.getWritableDir()
this.commandsPath = path.join(this.browserstackFolderPath, 'commands.json')
}

public static checkAndGetInstance() {
if (!AccessibilityScripts.instance) {
Expand All @@ -37,6 +40,30 @@ class AccessibilityScripts {
return AccessibilityScripts.instance
}

/* eslint-disable @typescript-eslint/no-unused-vars */
public getWritableDir(): string {
const orderedPaths = [
path.join(os.homedir(), '.browserstack'),
process.cwd(),
os.tmpdir()
]
for (const orderedPath of orderedPaths) {
try {
if (fs.existsSync(orderedPath)) {
fs.accessSync(orderedPath)
return orderedPath
}

fs.mkdirSync(orderedPath, { recursive: true })
return orderedPath

} catch (error) {
/* no-empty */
}
}
return ''
}

public readFromExistingFile() {
try {
if (fs.existsSync(this.commandsPath)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'node:fs'

import { describe, expect, it, vi, afterAll, beforeAll } from 'vitest'
import { describe, expect, it, vi, afterAll, beforeAll, beforeEach } from 'vitest'

import AccessibilityScripts from '../src/scripts/accessibility-scripts.js'

Expand All @@ -9,7 +9,8 @@ vi.mock('node:fs', () => ({
readFileSync: vi.fn().mockReturnValue('{"scripts": {"scan": "scan", "getResults": "getResults", "getResultsSummary": "getResultsSummary", "saveResults": "saveResults"}, "commands": [{"command": "command1"}, {"command": "command2"}]}'),
writeFileSync: vi.fn(),
existsSync: vi.fn().mockReturnValue(true),
mkdirSync: vi.fn()
mkdirSync: vi.fn(),
accessSync: vi.fn()
}
}))

Expand Down Expand Up @@ -80,3 +81,62 @@ describe('AccessibilityScripts', () => {
)
})
})

describe('getWritableDir', () => {
const accessibilityScripts: typeof AccessibilityScripts = AccessibilityScripts
const existsSyncStub: any = vi.spyOn(fs, 'existsSync')
const accessSyncStub: any = vi.spyOn(fs, 'accessSync')
const mkdirSyncStub: any = vi.spyOn(fs, 'mkdirSync')
let writableDir: string

beforeEach(() => {
existsSyncStub.mockReset()
accessSyncStub.mockReset()
mkdirSyncStub.mockReset()
})

it('should return a path when directory is present', () => {
existsSyncStub.mockReturnValue(true)
writableDir = accessibilityScripts.getWritableDir()
expect(existsSyncStub).toHaveBeenCalled()
expect(accessSyncStub).toHaveBeenCalled()
expect(writableDir).toBeTruthy()
})

it('should create the directory and return the path when it is not present', () => {
existsSyncStub.mockReturnValue(false)
writableDir = accessibilityScripts.getWritableDir()
expect(existsSyncStub).toHaveBeenCalled()
expect(mkdirSyncStub).toHaveBeenCalledWith(expect.any(String), { recursive: true })
expect(writableDir).toBeTruthy()
})

it('should return an empty string when mkdirSync throws an exception', () => {
existsSyncStub.mockReturnValue(false)
mkdirSyncStub.mockImplementation(() => {
throw new Error('Failed to create directory')
})

writableDir = accessibilityScripts.getWritableDir()
expect(existsSyncStub).toHaveBeenCalled()
expect(mkdirSyncStub).toHaveBeenCalled()
expect(writableDir).toBe('') // Expect empty string as fallback
})

it('should skip the first path if mkdirSync throws and succeed for the second path', () => {
let callCount = 0

existsSyncStub.mockImplementation(() => false)
mkdirSyncStub.mockImplementation(() => {
if (callCount === 0) {
callCount++
throw new Error('Failed to create first directory')
}
})

writableDir = accessibilityScripts.getWritableDir()
expect(existsSyncStub).toHaveBeenCalledTimes(2)
expect(mkdirSyncStub).toHaveBeenCalledTimes(2) // Called for first adn second paths
expect(writableDir).toBe(process.cwd()) // Should return the second path
})
})

0 comments on commit d57358e

Please sign in to comment.