Skip to content

Commit

Permalink
Merge pull request #15 from jecisc/14-Add-way-to-initialize-remove-ol…
Browse files Browse the repository at this point in the history
…d-log-like-ensureDelete-

14-Add-way-to-initialize-remove-old-log-like-ensureDelete-
  • Loading branch information
jecisc committed Jun 14, 2019
2 parents 25b41de + 01b6e3f commit 6529cc8
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 29 deletions.
6 changes: 0 additions & 6 deletions src/TinyLogger-Tests/NonInteractiveTranscript.extension.st

This file was deleted.

7 changes: 6 additions & 1 deletion src/TinyLogger-Tests/TinyAbstractLoggerTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ TinyAbstractLoggerTest >> actualClass [

{ #category : #helpers }
TinyAbstractLoggerTest >> skipInPharo6 [
self flag: #pharo6. "There is a bug with stdout in Pharo 6 and a hack to manage it. But it breaks the tests so we skip the tests using Stdio streams. Remove that when compatibility with P6 will be dropped."
self flag: #pharo6. "There is a bug with stdout in Pharo 6 and a hack to manage it. But it breaks the tests so we skip the tests using Stdio streams. Remove that when compatibility with P6 will be dropped."
SystemVersion current major < 7 ifTrue: [ self skip ]
]

{ #category : #test }
TinyAbstractLoggerTest >> testClearLogger [
self subclassResponsibility
]

{ #category : #test }
TinyAbstractLoggerTest >> testRecord [
self subclassResponsibility
Expand Down
48 changes: 27 additions & 21 deletions src/TinyLogger-Tests/TinyFileLoggerTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ A TinyFileLoggerTest is a test class for testing the behavior of TinyFileLogger
Class {
#name : #TinyFileLoggerTest,
#superclass : #TinyLeafLoggerTest,
#instVars : [
'log'
],
#category : #'TinyLogger-Tests-Core'
}

Expand All @@ -17,57 +20,60 @@ TinyFileLoggerTest >> fileNameNumber: number [
^ 'testFile' , number asString , 'ForTinyLogger.log'
]

{ #category : #running }
TinyFileLoggerTest >> tearDown [
log ifNotNil: #ensureDelete.
super tearDown
]

{ #category : #test }
TinyFileLoggerTest >> testClearLogger [
log := (self fileNameNumber: 1) asFileReference.
logger := self actualClass for: parentLogger named: log basename.
logger record: 'this is a test'.

self assert: log exists.
logger clearLogger.
self deny: log exists
]

{ #category : #test }
TinyFileLoggerTest >> testFileName [
| log |
log := (self fileNameNumber: 4) asFileReference.
[
logger := self actualClass for: parentLogger named: log basename.
logger record: 'this is a test'.
self assert: (self fileNameNumber: 4) asFileReference exists ]
ensure: [ (log isNotNil and: [ log exists ])
ifTrue: [ log ensureDelete ] ]

self assert: (self fileNameNumber: 4) asFileReference exists
]

{ #category : #test }
TinyFileLoggerTest >> testLoggerCreatesFile [
| log |
log := (self fileNameNumber: 10) asFileReference.
[
logger := self actualClass for: parentLogger named: log basename.
self deny: log exists.

logger record: 'this is a test'.
self assert: log exists ]
ensure: [ (log isNotNil and: [ log exists ])
ifTrue: [ log ensureDelete ] ]
self assert: log exists
]

{ #category : #test }
TinyFileLoggerTest >> testRecord [
| log |
log := (self fileNameNumber: 1) asFileReference.
[
logger := self actualClass for: parentLogger named: log basename.
logger record: 'this is a test'.

self assert: log exists.
self assert: log contents lines isNotEmpty.
self assert: (log contents lines last includesSubstring: 'this is a test') ]
ensure: [ (log isNotNil and: [ log exists ])
ifTrue: [ log ensureDelete ] ]
self assert: (log contents lines last includesSubstring: 'this is a test')
]

{ #category : #test }
TinyFileLoggerTest >> testRecordSetFileToTheEnd [
| log |
log := (self fileNameNumber: 1) asFileReference.
[
logger := self actualClass for: parentLogger named: log basename.
logger record: 'this is a test'.

self assert: log contents lines size equals: 1.
logger record: 'this is a test2'.
self assert: log contents lines size equals: 2 ]
ensure: [ (log isNotNil and: [ log exists ])
ifTrue: [ log ensureDelete ] ]
self assert: log contents lines size equals: 2
]
14 changes: 14 additions & 0 deletions src/TinyLogger-Tests/TinyLoggerTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ TinyLoggerTest >> testAddTranscriptLogger [
self assert: logger transcriptLoggers size equals: 2
]

{ #category : #test }
TinyLoggerTest >> testClearLogger [
logger
addTranscriptLogger;
addFileLoggerNamed: 'testFileForTinyLogger.log';
addFileLoggerNamed: 'testFileForTinyLogger2.log'.

logger record: 'Test'.
self assert: (logger fileLoggers allSatisfy: [ :fileLogger | fileLogger fileReference exists ]).

logger clearLogger.
self assert: (logger fileLoggers noneSatisfy: [ :fileLogger | fileLogger fileReference exists ])
]

{ #category : #test }
TinyLoggerTest >> testExecuteRecordedAs [
| contents stream bool |
Expand Down
5 changes: 5 additions & 0 deletions src/TinyLogger-Tests/TinyStdoutLoggerTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ TinyStdoutLoggerTest >> actualClass [
^ TinyStdoutLogger
]

{ #category : #test }
TinyStdoutLoggerTest >> testClearLogger [
"Do nothing because stdio cannot be clear"
]

{ #category : #test }
TinyStdoutLoggerTest >> testRecord [
| stream |
Expand Down
7 changes: 7 additions & 0 deletions src/TinyLogger/TinyAbstractLogger.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ TinyAbstractLogger >> << aString [
self record: aString
]

{ #category : #logging }
TinyAbstractLogger >> clearLogger [
"Whe called, this method should clear the previous logs"

self subclassResponsibility
]

{ #category : #logging }
TinyAbstractLogger >> record: aString [
self subclassResponsibility
Expand Down
12 changes: 11 additions & 1 deletion src/TinyLogger/TinyFileLogger.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ TinyFileLogger class >> named: aString [
yourself
]

{ #category : #logging }
TinyFileLogger >> clearLogger [
self fileReference ensureDelete
]

{ #category : #accessing }
TinyFileLogger >> fileName [
^ fileName ifNil: [ fileName := 'TinyLogger.log' ]
Expand All @@ -60,11 +65,16 @@ TinyFileLogger >> fileName: anObject [
fileName := anObject
]

{ #category : #accessing }
TinyFileLogger >> fileReference [
^ self fileName asFileReference
]

{ #category : #accessing }
TinyFileLogger >> fileStreamDo: aBlock [
"For now we re-open the file all the time to avoid problems when we change its image of computer/OS or if the user delete the file. Maybe later we can find a better solution but in any case we should test such weird behaviors."

^ self fileName asFileReference
^ self fileReference
ensureCreateFile;
writeStreamDo: [ :s |
s setToEnd.
Expand Down
5 changes: 5 additions & 0 deletions src/TinyLogger/TinyLogger.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ TinyLogger >> addTranscriptLogger [
self addLogger: TinyTranscriptLogger new
]

{ #category : #logging }
TinyLogger >> clearLogger [
self loggers do: #clearLogger
]

{ #category : #nesting }
TinyLogger >> decreaseDepthLevel [
self depth: self depth - 1
Expand Down
7 changes: 7 additions & 0 deletions src/TinyLogger/TinyStdoutLogger.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ TinyStdoutLogger class >> kind [
^ 'stdout'
]

{ #category : #logging }
TinyStdoutLogger >> clearLogger [
"We do nothing here since we cannot clear stdout"


]

{ #category : #initialization }
TinyStdoutLogger >> initialize [
super initialize.
Expand Down
5 changes: 5 additions & 0 deletions src/TinyLogger/TinyTranscriptLogger.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ TinyTranscriptLogger class >> kind [
^ 'transcript'
]

{ #category : #logging }
TinyTranscriptLogger >> clearLogger [
Transcript clear
]

{ #category : #logging }
TinyTranscriptLogger >> record: aString [
Transcript log: (String streamContents: [ :s | self record: aString on: s ])
Expand Down

0 comments on commit 6529cc8

Please sign in to comment.