Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add #getTemporaryDirectory to UnixPlatform and WinPlatform #13777

Merged
merged 10 commits into from
May 26, 2023
4 changes: 3 additions & 1 deletion src/FileSystem-Core/UnixResolver.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ UnixResolver >> preferences [

{ #category : #origins }
UnixResolver >> temp [
^ '/tmp' asFileReference

^self directoryFromEnvVariableNamed: 'TMPDIR' or: [
'/tmp' asFileReference]
]

{ #category : #origins }
Expand Down
3 changes: 2 additions & 1 deletion src/FileSystem-Core/WindowsResolver.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ WindowsResolver >> preferences [

{ #category : #origins }
WindowsResolver >> temp [
^ self directoryFromEnvVariableNamed: 'TEMP' or: [ FileLocator C / 'windows' / 'temp' ]

^ self resolveString: Smalltalk os getTempPath
]
14 changes: 14 additions & 0 deletions src/FileSystem-Tests-Core/UnixResolverTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ UnixResolverTest >> createResolver [
^ UnixResolver new
]

{ #category : #running }
UnixResolverTest >> testGetTempFromTMPDIR [

| expected |
OSPlatform current isUnix ifFalse: [ ^ self skip ].

expected := '/tmp/foo'.
OSEnvironment current setEnv: 'TMPDIR' value: expected during: [
| actual |
actual := self createResolver temp.
self assert: actual fullName equals: expected ]

]

{ #category : #tests }
UnixResolverTest >> testXdgParseUserDirLineDocuments [
"Ensure that a path of the form '$HOME/Documents' answers the expected value.
Expand Down
22 changes: 22 additions & 0 deletions src/FileSystem-Tests-Core/WindowsResolverTest.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"
A WindowsResolverTest is a test class for testing the behavior of WindowsResolver
"
Class {
#name : #WindowsResolverTest,
#superclass : #TestCase,
#category : #'FileSystem-Tests-Core-Resolver'
}

{ #category : #tests }
WindowsResolverTest >> testResolveTempPathFromTMP [

| expected |
OSPlatform current isWindows ifFalse: [ ^ self skip ].

expected := 'X:\Temp'.

OSEnvironment current setEnv: 'TMP' value: expected during: [
| actual |
actual := WindowsResolver new temp.
self assert: actual fullName equals: expected ]
]
47 changes: 47 additions & 0 deletions src/System-OSEnvironments-Tests/OSEnvironmentTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,24 @@ Class {
#category : #'System-OSEnvironments-Tests'
}

{ #category : #helper }
OSEnvironmentTest >> envNameForTest [

^ 'PHAROTEST'
]

{ #category : #helper }
OSEnvironmentTest >> instance [
^ Smalltalk os environment
]

{ #category : #running }
OSEnvironmentTest >> tearDown [

self instance removeKey: self envNameForTest.
super tearDown
]

{ #category : #tests }
OSEnvironmentTest >> testAsDictionary [
self assert: self instance asDictionary isDictionary
Expand Down Expand Up @@ -75,6 +88,40 @@ OSEnvironmentTest >> testKeys [
self assert: (env includesKey: keys anyOne)
]

{ #category : #tests }
OSEnvironmentTest >> testSettingEnvValueDuringChangesValueInDuringBlock [

| envName expected |
envName := self envNameForTest.
self instance setEnv: envName value: 'Before'.
expected := 'After'.
self instance setEnv: envName value: expected during: [
| actual |
actual := self instance at: envName.
self assert: actual equals: expected ]
]

{ #category : #tests }
OSEnvironmentTest >> testSettingEnvValueDuringRevertsValueAfterDuringBlock [

| actual envName expected |
envName := self envNameForTest.
self instance setEnv: envName value: 'Before'.
expected := 'After'.
self instance setEnv: envName value: expected during: [ ].
actual := self instance at: envName.
self assert: actual equals: expected
]

{ #category : #tests }
OSEnvironmentTest >> testSettingNewEnvValueDuringRemovesItAfterDuringBlock [

| envName |
envName := self envNameForTest.
self instance setEnv: envName value: 'During' during: [ ].
self deny: (self instance includesKey: envName)
]

{ #category : #tests }
OSEnvironmentTest >> testValues [
| env values |
Expand Down
13 changes: 13 additions & 0 deletions src/System-OSEnvironments/OSEnvironment.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,19 @@ OSEnvironment >> setEnv: nameString value: valueString [
^ self subclassResponsibility
]

{ #category : #accessing }
OSEnvironment >> setEnv: nameString value: valueString during: aBlock [

| oldValue |
oldValue := self at: nameString ifAbsent: [ nil ].
[
self setEnv: nameString value: valueString.
aBlock value ] ensure: [
oldValue
ifNil: [ self removeKey: nameString ]
ifNotNil: [ self setEnv: nameString value: valueString ] ]
]

{ #category : #accessing }
OSEnvironment >> unsetEnv: string [
"This method calls the the platform specific unset environment routine"
Expand Down
2 changes: 1 addition & 1 deletion src/System-Platforms-Tests/Win32EnvironmentTest.class.st
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Class {
#name : #Win32EnvironmentTest,
#superclass : #TestCase,
#category : #'System-Platforms-Tests-Win32'
#category : #'System-Platforms-Tests-Windows'
}

{ #category : #tests }
Expand Down
2 changes: 1 addition & 1 deletion src/System-Platforms-Tests/Win32WideStringTest.class.st
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Class {
#name : #Win32WideStringTest,
#superclass : #TestCase,
#category : #'System-Platforms-Tests-Win32'
#category : #'System-Platforms-Tests-Windows'
}

{ #category : #tests }
Expand Down
21 changes: 21 additions & 0 deletions src/System-Platforms-Tests/WinPlatformTest.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Class {
#name : #WinPlatformTest,
#superclass : #TestCase,
#category : #'System-Platforms-Tests-Windows'
}

{ #category : #tests }
WinPlatformTest >> testGetTempPathFromTMP [

| value |
OSPlatform current isWindows ifFalse: [ ^ self skip ].

value := OSEnvironment current at: 'TMP' ifAbsent: [ nil ].
[
| actual expected |
expected := 'X:\Temp\'.
OSEnvironment current at: 'TMP' put: expected.
actual := OSPlatform current getTempPath.
self assert: actual equals: expected ] ensure: [
value ifNotNil: [ OSEnvironment current at: 'TMP' put: value ] ]
]
17 changes: 17 additions & 0 deletions src/System-Platforms/WinPlatform.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ WinPlatform >> getErrorMessage: anInteger [
^ buffer asString
]

{ #category : #'file paths' }
WinPlatform >> getTempPath [

| buffer length |
length := self defaultMaximumPathLength.
buffer := (String new: length) asWin32WideString.
self getTempPath: length buffer: buffer.
^buffer asString
]

{ #category : #'file paths' }
WinPlatform >> getTempPath: bufferLength buffer: buffer [

self ffiCall: #(long GetTempPath2W(long bufferLength,
void* buffer))
]

{ #category : #testing }
WinPlatform >> isWindows [
^ true
Expand Down