-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
317 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package offline_test | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"os" | ||
"testing" | ||
|
||
cmdoffline "github.com/wakatime/wakatime-cli/cmd/offline" | ||
"github.com/wakatime/wakatime-cli/pkg/heartbeat" | ||
"github.com/wakatime/wakatime-cli/pkg/offline" | ||
|
||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSaveHeartbeats(t *testing.T) { | ||
tmpFile, err := os.CreateTemp(t.TempDir(), "") | ||
require.NoError(t, err) | ||
|
||
defer tmpFile.Close() | ||
|
||
offlineQueueFile, err := os.CreateTemp(t.TempDir(), "") | ||
require.NoError(t, err) | ||
|
||
defer offlineQueueFile.Close() | ||
|
||
ctx := context.Background() | ||
|
||
v := viper.New() | ||
v.Set("config", tmpFile.Name()) | ||
v.Set("category", "debugging") | ||
v.Set("cursorpos", 42) | ||
v.Set("entity", "testdata/main.go") | ||
v.Set("entity-type", "file") | ||
v.Set("key", "00000000-0000-4000-8000-000000000000") | ||
v.Set("language", "Go") | ||
v.Set("alternate-language", "Golang") | ||
v.Set("hide-branch-names", true) | ||
v.Set("project", "wakatime-cli") | ||
v.Set("lineno", 13) | ||
v.Set("time", 1585598059.1) | ||
v.Set("timeout", 5) | ||
v.Set("write", true) | ||
|
||
err = cmdoffline.SaveHeartbeats(ctx, v, nil, offlineQueueFile.Name()) | ||
require.NoError(t, err) | ||
|
||
offlineCount, err := offline.CountHeartbeats(ctx, offlineQueueFile.Name()) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, 1, offlineCount) | ||
} | ||
|
||
func TestSaveHeartbeats_ExtraHeartbeats(t *testing.T) { | ||
tmpFile, err := os.CreateTemp(t.TempDir(), "") | ||
require.NoError(t, err) | ||
|
||
defer tmpFile.Close() | ||
|
||
offlineQueueFile, err := os.CreateTemp(t.TempDir(), "") | ||
require.NoError(t, err) | ||
|
||
defer offlineQueueFile.Close() | ||
|
||
ctx := context.Background() | ||
|
||
data, err := os.ReadFile("testdata/extra_heartbeats.json") | ||
require.NoError(t, err) | ||
|
||
var hh []heartbeat.Heartbeat | ||
|
||
err = json.Unmarshal(data, &hh) | ||
require.NoError(t, err) | ||
|
||
v := viper.New() | ||
v.Set("config", tmpFile.Name()) | ||
v.Set("entity", "testdata/main.go") | ||
v.Set("key", "00000000-0000-4000-8000-000000000000") | ||
|
||
err = cmdoffline.SaveHeartbeats(ctx, v, hh, offlineQueueFile.Name()) | ||
require.NoError(t, err) | ||
|
||
offlineCount, err := offline.CountHeartbeats(ctx, offlineQueueFile.Name()) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, 25, offlineCount) | ||
} | ||
|
||
func TestSaveHeartbeats_OfflineDisabled(t *testing.T) { | ||
tmpFile, err := os.CreateTemp(t.TempDir(), "") | ||
require.NoError(t, err) | ||
|
||
defer tmpFile.Close() | ||
|
||
offlineQueueFile, err := os.CreateTemp(t.TempDir(), "") | ||
require.NoError(t, err) | ||
|
||
defer offlineQueueFile.Close() | ||
|
||
ctx := context.Background() | ||
|
||
v := viper.New() | ||
v.Set("config", tmpFile.Name()) | ||
v.Set("disable-offline", true) | ||
v.Set("entity", "testdata/main.go") | ||
v.Set("key", "00000000-0000-4000-8000-000000000000") | ||
|
||
err = cmdoffline.SaveHeartbeats(ctx, v, nil, offlineQueueFile.Name()) | ||
|
||
assert.EqualError(t, err, "saving to offline db disabled") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[ { "alternate_language": "Golang", "alternate_project": "billing", "category": "coding", "cursorpos": 12, "entity": "testdata/main.go", "entity_type": "file", "is_write": true, "language": "Go", "lineno": 42, "lines": 45, "project": "wakatime-cli", "time": 1585598059 }, { "alternate_language": "Py", "category": "debugging", "cursorpos": null, "entity": "testdata/main.py", "is_write": null, "language": "Python", "lineno": null, "lines": null, "project": "wakatime-cli", "type": "file", "timestamp": 1585598060 }, { "alternate_language": "Golang", "alternate_project": "billing", "category": "coding", "cursorpos": 12, "entity": "testdata/main.go", "entity_type": "file", "is_write": true, "language": "Go", "lineno": 42, "lines": 45, "project": "wakatime-cli", "time": 1585598061 }, { "alternate_language": "Golang", "alternate_project": "billing", "category": "coding", "cursorpos": 12, "entity": "testdata/main.go", "entity_type": "file", "is_write": true, "language": "Go", "lineno": 42, "lines": 45, "project": "wakatime-cli", "time": 1585598062 }, { "alternate_language": "Golang", "alternate_project": "billing", "category": "coding", "cursorpos": 12, "entity": "testdata/main.go", "entity_type": "file", "is_write": true, "language": "Go", "lineno": 42, "lines": 45, "project": "wakatime-cli", "time": 1585598063 }, { "alternate_language": "Golang", "alternate_project": "billing", "category": "coding", "cursorpos": 12, "entity": "testdata/main.go", "entity_type": "file", "is_write": true, "language": "Go", "lineno": 42, "lines": 45, "project": "wakatime-cli", "time": 1585598064 }, { "alternate_language": "Golang", "alternate_project": "billing", "category": "coding", "cursorpos": 12, "entity": "testdata/main.go", "entity_type": "file", "is_write": true, "language": "Go", "lineno": 42, "lines": 45, "project": "wakatime-cli", "time": 1585598065 }, { "alternate_language": "Golang", "alternate_project": "billing", "category": "coding", "cursorpos": 12, "entity": "testdata/main.go", "entity_type": "file", "is_write": true, "language": "Go", "lineno": 42, "lines": 45, "project": "wakatime-cli", "time": 1585598066 }, { "alternate_language": "Golang", "alternate_project": "billing", "category": "coding", "cursorpos": 12, "entity": "testdata/main.go", "entity_type": "file", "is_write": true, "language": "Go", "lineno": 42, "lines": 45, "project": "wakatime-cli", "time": 1585598067 }, { "alternate_language": "Golang", "alternate_project": "billing", "category": "coding", "cursorpos": 12, "entity": "testdata/main.go", "entity_type": "file", "is_write": true, "language": "Go", "lineno": 42, "lines": 45, "project": "wakatime-cli", "time": 1585598068 }, { "alternate_language": "Golang", "alternate_project": "billing", "category": "coding", "cursorpos": 12, "entity": "testdata/main.go", "entity_type": "file", "is_write": true, "language": "Go", "lineno": 42, "lines": 45, "project": "wakatime-cli", "time": 1585598069 }, { "alternate_language": "Golang", "alternate_project": "billing", "category": "coding", "cursorpos": 12, "entity": "testdata/main.go", "entity_type": "file", "is_write": true, "language": "Go", "lineno": 42, "lines": 45, "project": "wakatime-cli", "time": 1585598070 }, { "alternate_language": "Golang", "alternate_project": "billing", "category": "coding", "cursorpos": 12, "entity": "testdata/main.go", "entity_type": "file", "is_write": true, "language": "Go", "lineno": 42, "lines": 45, "project": "wakatime-cli", "time": 1585598071 }, { "alternate_language": "Golang", "alternate_project": "billing", "category": "coding", "cursorpos": 12, "entity": "testdata/main.go", "entity_type": "file", "is_write": true, "language": "Go", "lineno": 42, "lines": 45, "project": "wakatime-cli", "time": 1585598072 }, { "alternate_language": "Golang", "alternate_project": "billing", "category": "coding", "cursorpos": 12, "entity": "testdata/main.go", "entity_type": "file", "is_write": true, "language": "Go", "lineno": 42, "lines": 45, "project": "wakatime-cli", "time": 1585598073 }, { "alternate_language": "Golang", "alternate_project": "billing", "category": "coding", "cursorpos": 12, "entity": "testdata/main.go", "entity_type": "file", "is_write": true, "language": "Go", "lineno": 42, "lines": 45, "project": "wakatime-cli", "time": 1585598074 }, { "alternate_language": "Golang", "alternate_project": "billing", "category": "coding", "cursorpos": 12, "entity": "testdata/main.go", "entity_type": "file", "is_write": true, "language": "Go", "lineno": 42, "lines": 45, "project": "wakatime-cli", "time": 1585598075 }, { "alternate_language": "Golang", "alternate_project": "billing", "category": "coding", "cursorpos": 12, "entity": "testdata/main.go", "entity_type": "file", "is_write": true, "language": "Go", "lineno": 42, "lines": 45, "project": "wakatime-cli", "time": 1585598076 }, { "alternate_language": "Golang", "alternate_project": "billing", "category": "coding", "cursorpos": 12, "entity": "testdata/main.go", "entity_type": "file", "is_write": true, "language": "Go", "lineno": 42, "lines": 45, "project": "wakatime-cli", "time": 1585598077 }, { "alternate_language": "Golang", "alternate_project": "billing", "category": "coding", "cursorpos": 12, "entity": "testdata/main.go", "entity_type": "file", "is_write": true, "language": "Go", "lineno": 42, "lines": 45, "project": "wakatime-cli", "time": 1585598078 }, { "alternate_language": "Golang", "alternate_project": "billing", "category": "coding", "cursorpos": 12, "entity": "testdata/main.go", "entity_type": "file", "is_write": true, "language": "Go", "lineno": 42, "lines": 45, "project": "wakatime-cli", "time": 1585598079 }, { "alternate_language": "Golang", "alternate_project": "billing", "category": "coding", "cursorpos": 12, "entity": "testdata/main.go", "entity_type": "file", "is_write": true, "language": "Go", "lineno": 42, "lines": 45, "project": "wakatime-cli", "time": 1585598080 }, { "alternate_language": "Golang", "alternate_project": "billing", "category": "coding", "cursorpos": 12, "entity": "testdata/main.go", "entity_type": "file", "is_write": true, "language": "Go", "lineno": 42, "lines": 45, "project": "wakatime-cli", "time": 1585598081 }, { "alternate_language": "Golang", "alternate_project": "billing", "category": "coding", "cursorpos": 12, "entity": "testdata/main.go", "entity_type": "file", "is_write": true, "language": "Go", "lineno": 42, "lines": 45, "project": "wakatime-cli", "time": 1585598082 }, { "alternate_language": "Golang", "alternate_project": "billing", "category": "coding", "cursorpos": 12, "entity": "testdata/main.go", "entity_type": "file", "is_write": true, "language": "Go", "lineno": 42, "lines": 45, "project": "wakatime-cli", "time": 1585598083 } ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
hello | ||
hello | ||
world |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func main() { | ||
fmt.Println("hello world") | ||
os.Exit(0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# -*- coding: utf-8 -*- | ||
# vim: set filetype=python | ||
|
||
from __future__ import print_statement | ||
|
||
import os, sys | ||
from flask import session, render_template | ||
import simplejson as json | ||
|
||
|
||
class MyClass(object): | ||
"""this class | ||
""" | ||
|
||
def method1(self): | ||
a = 1 + 2 | ||
b = 'hello world!' | ||
for x in y: | ||
print(x) | ||
raise Exception() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters